Skip to content

Commit 0b9c657

Browse files
committed
[IO-859] FileUtils.forceDelete on non-existent file on Windows throws
IOException rather than FileNotFoundException
1 parent 8518255 commit 0b9c657

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

src/main/java/org/apache/commons/io/FileUtils.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -285,17 +285,19 @@ public static String byteCountToDisplaySize(final Number size) {
285285
}
286286

287287
/**
288-
* Requires that the given {@link File} object
289-
* points to an actual file (not a directory) in the file system,
290-
* and throws a {@link FileNotFoundException} if it doesn't.
291-
* It throws an IllegalArgumentException if the object points to a directory.
288+
* Requires that the given {@link File} exists, and throws a {@link FileNotFoundException} if it doesn't.
292289
*
293290
* @param file The {@link File} to check.
294-
* @param name The parameter name to use in the exception message.
295291
* @throws FileNotFoundException if the file does not exist
296-
* @throws NullPointerException if the given {@link File} is {@code null}.
297-
* @throws IllegalArgumentException if the given {@link File} is not a file.
292+
* @throws NullPointerException if the given {@link File} is {@code null}.
298293
*/
294+
private static void checkExists(final File file) throws FileNotFoundException {
295+
Objects.requireNonNull(file, "file");
296+
if (!file.exists()) {
297+
throw new FileNotFoundException(file.toString());
298+
}
299+
}
300+
299301
private static void checkFileExists(final File file, final String name) throws FileNotFoundException {
300302
Objects.requireNonNull(file, name);
301303
if (!file.isFile()) {
@@ -1385,7 +1387,7 @@ private static void doCopyDirectory(final File srcDir, final File destDir, final
13851387
*/
13861388
public static void forceDelete(final File file) throws IOException {
13871389
Objects.requireNonNull(file, PROTOCOL_FILE);
1388-
checkFileExists(file, file.getName()); // fail-fast
1390+
checkExists(file); // fail-fast
13891391
final Counters.PathCounters deleteCounters;
13901392
try {
13911393
deleteCounters = PathUtils.delete(file.toPath(), PathUtils.EMPTY_LINK_OPTION_ARRAY, StandardDeleteOption.OVERRIDE_READ_ONLY);

0 commit comments

Comments
 (0)