This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-io.git


The following commit(s) were added to refs/heads/master by this push:
     new 45b15e4  IO-629: Providing more meaningful exceptions on file delete 
(#98)
45b15e4 is described below

commit 45b15e4fcd2c49eebc56b881b61893402bcd25dd
Author: malfist <[email protected]>
AuthorDate: Mon Oct 28 12:47:41 2019 -0500

    IO-629: Providing more meaningful exceptions on file delete (#98)
    
    * IO-629: Providing more meaningful exceptions on file delete
    
    * IS-629: Refactoring FileUtils#forceDelete to use PathUtils delete api
    
    * IO-629: Providing more meaningful exceptions on file delete
    
    * IS-629: Refactoring FileUtils#forceDelete to use PathUtils delete api
    
    * IO-629: Providing more meaningful exceptions on file delete
    
    * IO-629: Fixing checkstyle
---
 src/main/java/org/apache/commons/io/FileUtils.java | 25 +++++++++++-----------
 1 file changed, 13 insertions(+), 12 deletions(-)

diff --git a/src/main/java/org/apache/commons/io/FileUtils.java 
b/src/main/java/org/apache/commons/io/FileUtils.java
index c3eaeb5..4e46021 100644
--- a/src/main/java/org/apache/commons/io/FileUtils.java
+++ b/src/main/java/org/apache/commons/io/FileUtils.java
@@ -45,6 +45,8 @@ import java.util.zip.CRC32;
 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;
@@ -1331,18 +1333,17 @@ public class FileUtils {
      * @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();
-            if (!file.delete()) {
-                if (!filePresent) {
-                    throw new FileNotFoundException("File does not exist: " + 
file);
-                }
-                final String message =
-                        "Unable to delete file: " + file;
-                throw new IOException(message);
-            }
+        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);
         }
     }
 

Reply via email to