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

commit be64c1642913dbd2d2840a5a2bafab363349c43e
Author: Gary Gregory <gardgreg...@gmail.com>
AuthorDate: Fri Jan 8 12:17:54 2021 -0500

    Add FileUtils.delete(File), refactor, reuse.
    
    Javadoc.
---
 src/changes/changes.xml                              |  3 +++
 .../org/apache/commons/io/FileDeleteStrategy.java    |  9 +++++----
 src/main/java/org/apache/commons/io/FileUtils.java   | 20 +++++++++++++++++---
 .../org/apache/commons/io/FileUtilsTestCase.java     |  6 ++++++
 4 files changed, 31 insertions(+), 7 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 2f1601c..2396442 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -115,6 +115,9 @@ The <action> type attribute can be add,update,fix,remove.
       <action dev="ggregory" type="add" due-to="Gary Gregory">
         Add FileSystem#supportsDriveLetter().
       </action>
+      <action dev="ggregory" type="add" due-to="Gary Gregory">
+        Add FileUtils.delete(File).
+      </action>
       <!-- UPDATES -->
       <action dev="ggregory" type="update" due-to="Dependabot">
         Update junit-jupiter from 5.6.2 to 5.7.0 #153.
diff --git a/src/main/java/org/apache/commons/io/FileDeleteStrategy.java 
b/src/main/java/org/apache/commons/io/FileDeleteStrategy.java
index 244c53d..31fbdf2 100644
--- a/src/main/java/org/apache/commons/io/FileDeleteStrategy.java
+++ b/src/main/java/org/apache/commons/io/FileDeleteStrategy.java
@@ -135,16 +135,17 @@ public class FileDeleteStrategy {
      * A check has been made to ensure that the file will exist.
      * </p>
      * <p>
-     * This implementation uses {@link File#delete()}.
+     * This implementation uses {@link FileUtils#delete(File)}.
      * </p>
      *
-     * @param fileToDelete  the file to delete, exists, not null
+     * @param file  the file to delete, exists, not null
      * @return true if the file was deleted
      * @throws NullPointerException if the file is null
      * @throws IOException if an error occurs during file deletion
      */
-    protected boolean doDelete(final File fileToDelete) throws IOException {
-        return fileToDelete.delete();
+    protected boolean doDelete(final File file) throws IOException {
+        FileUtils.delete(file);
+        return true;
     }
 
     /**
diff --git a/src/main/java/org/apache/commons/io/FileUtils.java 
b/src/main/java/org/apache/commons/io/FileUtils.java
index 49d87d0..4a64f2d 100644
--- a/src/main/java/org/apache/commons/io/FileUtils.java
+++ b/src/main/java/org/apache/commons/io/FileUtils.java
@@ -1109,6 +1109,22 @@ public class FileUtils {
     }
 
     /**
+     * Deletes the given File but throws IOException if it cannot, unlike 
{@link File#delete()}.
+     *
+     * @param file The file to delete.
+     * @return the given file.
+     * @throws IOException if the file cannot be deleted.
+     * @see File#delete()
+     * @since 2.9.0
+     */
+    public static File delete(final File file) throws IOException {
+        if (!file.delete()) {
+            throw new IOException("Unable to delete " + file);
+        }
+        return file;
+    }
+
+    /**
      * Deletes a directory recursively.
      *
      * @param directory directory to delete
@@ -1124,9 +1140,7 @@ public class FileUtils {
             cleanDirectory(directory);
         }
 
-        if (!directory.delete()) {
-            throw new IOException("Unable to delete directory " + directory + 
".");
-        }
+        delete(directory);
     }
 
     /**
diff --git a/src/test/java/org/apache/commons/io/FileUtilsTestCase.java 
b/src/test/java/org/apache/commons/io/FileUtilsTestCase.java
index e6aa9c4..b8a3798 100644
--- a/src/test/java/org/apache/commons/io/FileUtilsTestCase.java
+++ b/src/test/java/org/apache/commons/io/FileUtilsTestCase.java
@@ -1332,6 +1332,12 @@ public class FileUtilsTestCase {
     }
 
     @Test
+    public void testDelete() throws Exception {
+        assertEquals(testFile1, FileUtils.delete(testFile1));
+        assertThrows(IOException.class, () -> FileUtils.delete(new File("does 
not exist.nope")));
+    }
+
+    @Test
     public void testDeleteQuietlyDir() throws IOException {
         final File testDirectory = new File(temporaryFolder, 
"testDeleteQuietlyDir");
         final File testFile = new File(testDirectory, "testDeleteQuietlyFile");

Reply via email to