Skip to content

Commit

Permalink
IS-629: Refactoring FileUtils#forceDelete to use PathUtils delete api
Browse files Browse the repository at this point in the history
  • Loading branch information
Jerome Hollon committed Oct 28, 2019
1 parent 243e10c commit b0bf857
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions src/main/java/org/apache/commons/io/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
Expand All @@ -46,6 +45,8 @@
import java.util.zip.CheckedInputStream;
import java.util.zip.Checksum;

import org.apache.commons.io.file.Counters;
import org.apache.commons.io.file.PathUtils;
import org.apache.commons.io.filefilter.DirectoryFileFilter;
import org.apache.commons.io.filefilter.FalseFileFilter;
import org.apache.commons.io.filefilter.FileFilterUtils;
Expand Down Expand Up @@ -1332,19 +1333,17 @@ private static void doCopyFile(final File srcFile, final File destFile, final bo
* @throws IOException in case deletion is unsuccessful
*/
public static void forceDelete(final File file) throws IOException {
if (file.isDirectory()) {
deleteDirectory(file);
} else {
final boolean filePresent = file.exists();
try {
Files.delete(file.toPath());
} catch (NoSuchFileException e) {
// Throw FileNotFoundException for backwards compatibility.
throw new FileNotFoundException("File does not exist: " + file);
} catch (IOException e) {
final String message = "Unable to delete file: " + file;
throw new IOException(message, e);
}
Counters.PathCounters deleteCounter;
try {
deleteCounter = PathUtils.delete(file.toPath());
} catch (IOException e) {
final String message = "Unable to delete file: " + file;
throw new IOException(message, e);
}

if(deleteCounter.getFileCounter().get() < 1 && deleteCounter.getDirectoryCounter().get() < 1) {
// didn't find a file to delete.
throw new FileNotFoundException("File does not exist: " + file);
}
}

Expand Down

0 comments on commit b0bf857

Please sign in to comment.