Skip to content

Commit

Permalink
IO-135 - Add convenience deleteQuietly to FileUtils - adapted from pa…
Browse files Browse the repository at this point in the history
…tch by Kevin Conaway

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/io/trunk@609253 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
Niall Pemberton committed Jan 6, 2008
1 parent 75e31c0 commit eafc20d
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
2 changes: 2 additions & 0 deletions RELEASE-NOTES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ Bug fixes from 1.3.2

Enhancements from 1.3.2
-----------------------
- FileUtils
- Add a deleteQuietly method [IO-135]

- TeeInputStream [IO-129]
- Add new Tee input stream implementation
Expand Down
33 changes: 33 additions & 0 deletions src/java/org/apache/commons/io/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -892,6 +892,39 @@ public static void deleteDirectory(File directory) throws IOException {
}
}

/**
* Delete a file. If file is a directory, delete it and all sub-directories.
* <p>
* The difference between File.delete() and this method are:
* <ul>
* <li>A directory to be deleted does not have to be empty.</li>
* <li>No exceptions are thrown when a file or directory cannot be deleted.
* </ul>
*
* @param file file or directory to delete, can be <code>null</code>
* @return <code>true</code> if the file or directory was deleted, otherwise
* <code>false</code>
*
* @since Commons IO 1.4
*/
public static boolean deleteQuietly(File file) {
if (file == null) {
return false;
}
try {
if (file.isDirectory()) {
cleanDirectory(file);
}
} catch (Throwable t) {
}

try {
return file.delete();
} catch (Throwable t) {
return false;
}
}

/**
* Clean a directory without deleting it.
*
Expand Down
41 changes: 41 additions & 0 deletions src/test/org/apache/commons/io/FileUtilsTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -1242,6 +1242,47 @@ public void testChecksumDouble() throws Exception {
assertEquals(expectedValue, resultValue);
}

public void testDeleteQuietlyForNull() {
try {
FileUtils.deleteQuietly(null);
} catch (Exception ex) {
fail(ex.getMessage());
}
}

public void testDeleteQuietlyDir() throws IOException {
File testDirectory = new File(getTestDirectory(), "testDeleteQuietlyDir");
File testFile= new File(testDirectory, "testDeleteQuietlyFile");
testDirectory.mkdirs();
createFile(testFile, 0);

assertTrue(testDirectory.exists());
assertTrue(testFile.exists());
FileUtils.deleteQuietly(testDirectory);
assertFalse("Check No Exist", testDirectory.exists());
assertFalse("Check No Exist", testFile.exists());
}

public void testDeleteQuietlyFile() throws IOException {
File testFile= new File(getTestDirectory(), "testDeleteQuietlyFile");
createFile(testFile, 0);

assertTrue(testFile.exists());
FileUtils.deleteQuietly(testFile);
assertFalse("Check No Exist", testFile.exists());
}

public void testDeleteQuietlyNonExistent() {
File testFile = new File("testDeleteQuietlyNonExistent");
assertFalse(testFile.exists());

try {
FileUtils.deleteQuietly(testFile);
} catch (Exception ex) {
fail(ex.getMessage());
}
}

/**
* DirectoryWalker implementation that recursively lists all files and directories.
*/
Expand Down

0 comments on commit eafc20d

Please sign in to comment.