@@ -285,19 +285,28 @@ public static String byteCountToDisplaySize(final Number size) {
285285 }
286286
287287 /**
288- * Requires that the given {@link File} exists, and throws a {@link FileNotFoundException} if it doesn't .
288+ * Requires that the given {@link File} is non-null and exists ( if strict is true) .
289289 *
290290 * @param file The {@link File} to check.
291- * @throws FileNotFoundException if the file does not exist
291+ * @param strict whether to check that the {@code file} exists.
292+ * @throws FileNotFoundException if the file does not exist.
292293 * @throws NullPointerException if the given {@link File} is {@code null}.
293294 */
294- private static void checkExists (final File file ) throws FileNotFoundException {
295- Objects .requireNonNull (file , "file" );
296- if (!file .exists ()) {
295+ private static void checkExists (final File file , final boolean strict ) throws FileNotFoundException {
296+ Objects .requireNonNull (file , PROTOCOL_FILE );
297+ if (strict && !file .exists ()) {
297298 throw new FileNotFoundException (file .toString ());
298299 }
299300 }
300301
302+ /**
303+ * Requires that the given {@link File} exists, and throws a {@link FileNotFoundException} if it doesn't.
304+ *
305+ * @param file The {@link File} to check.
306+ * @param name The NullPointerException message.
307+ * @throws FileNotFoundException if the file does not exist.
308+ * @throws NullPointerException if the given {@link File} is {@code null}.
309+ */
301310 private static void checkFileExists (final File file , final String name ) throws FileNotFoundException {
302311 Objects .requireNonNull (file , name );
303312 if (!file .isFile ()) {
@@ -350,8 +359,8 @@ public static Checksum checksum(final File file, final Checksum checksum) throws
350359 *
351360 * @param file the file to checksum, must not be {@code null}
352361 * @return the checksum value
353- * @throws NullPointerException if the given {@link File } is {@code null}.
354- * @throws IllegalArgumentException if the given {@link File } does not exist or is not a file.
362+ * @throws NullPointerException if the {@code file } is {@code null}.
363+ * @throws IllegalArgumentException if the {@code file } does not exist or is not a file.
355364 * @throws IOException if an IO error occurs reading the file.
356365 * @since 1.3
357366 */
@@ -364,20 +373,20 @@ public static long checksumCRC32(final File file) throws IOException {
364373 *
365374 * @param directory directory to clean
366375 * @throws NullPointerException if the given {@link File} is {@code null}.
367- * @throws IllegalArgumentException if directory does not exist or is not a directory.
376+ * @throws IllegalArgumentException if the {@code directory} does not exist or is not a directory.
368377 * @throws IOException if an I/O error occurs.
369378 * @see #forceDelete(File)
370379 */
371380 public static void cleanDirectory (final File directory ) throws IOException {
372- IOConsumer .forAll (FileUtils :: forceDelete , listFiles (directory , null ));
381+ IOConsumer .forAll (f -> forceDelete ( f , false ) , listFiles (directory , null ));
373382 }
374383
375384 /**
376385 * Cleans a directory without deleting it.
377386 *
378387 * @param directory directory to clean, must not be {@code null}
379388 * @throws NullPointerException if the given {@link File} is {@code null}.
380- * @throws IllegalArgumentException if directory does not exist or is not a directory.
389+ * @throws IllegalArgumentException if the {@code directory} does not exist or is not a directory.
381390 * @throws IOException if an I/O error occurs.
382391 * @see #forceDeleteOnExit(File)
383392 */
@@ -1386,8 +1395,28 @@ private static void doCopyDirectory(final File srcDir, final File destDir, final
13861395 * @throws IOException in case deletion is unsuccessful.
13871396 */
13881397 public static void forceDelete (final File file ) throws IOException {
1398+ forceDelete (file , true );
1399+ }
1400+
1401+ /**
1402+ * Deletes a file or directory. For a directory, delete it and all subdirectories.
1403+ * <p>
1404+ * The difference between File.delete() and this method are:
1405+ * </p>
1406+ * <ul>
1407+ * <li>The directory does not have to be empty.</li>
1408+ * <li>You get an exception when a file or directory cannot be deleted.</li>
1409+ * </ul>
1410+ *
1411+ * @param file file or directory to delete, must not be {@code null}.
1412+ * @param strict whether to throw a FileNotFoundException.
1413+ * @throws NullPointerException if the file is {@code null}.
1414+ * @throws FileNotFoundException if the file was not found.
1415+ * @throws IOException in case deletion is unsuccessful.
1416+ */
1417+ private static void forceDelete (final File file , final boolean strict ) throws IOException {
13891418 Objects .requireNonNull (file , PROTOCOL_FILE );
1390- checkExists (file ); // fail-fast
1419+ checkExists (file , strict ); // fail-fast
13911420 final Counters .PathCounters deleteCounters ;
13921421 try {
13931422 deleteCounters = PathUtils .delete (file .toPath (), PathUtils .EMPTY_LINK_OPTION_ARRAY , StandardDeleteOption .OVERRIDE_READ_ONLY );
@@ -2265,11 +2294,11 @@ private static AccumulatorPathVisitor listAccumulate(final File directory, final
22652294 /**
22662295 * Lists files in a directory, asserting that the supplied directory exists and is a directory.
22672296 *
2268- * @param directory The directory to list
2297+ * @param directory The directory to list.
22692298 * @param fileFilter Optional file filter, may be null.
22702299 * @return The files in the directory, never {@code null}.
22712300 * @throws NullPointerException if directory is {@code null}.
2272- * @throws IllegalArgumentException if {@link directory} exists but is not a directory
2301+ * @throws IllegalArgumentException if {@link directory} exists but is not a directory.
22732302 * @throws IOException if an I/O error occurs.
22742303 */
22752304 private static File [] listFiles (final File directory , final FileFilter fileFilter ) throws IOException {
0 commit comments