Author: niallp
Date: Sat Jan 5 18:28:01 2008
New Revision: 609253
URL: http://svn.apache.org/viewvc?rev=609253&view=rev
Log:
IO-135 - Add convenience deleteQuietly to FileUtils - adapted from patch by
Kevin Conaway
Modified:
commons/proper/io/trunk/RELEASE-NOTES.txt
commons/proper/io/trunk/src/java/org/apache/commons/io/FileUtils.java
commons/proper/io/trunk/src/test/org/apache/commons/io/FileUtilsTestCase.java
Modified: commons/proper/io/trunk/RELEASE-NOTES.txt
URL:
http://svn.apache.org/viewvc/commons/proper/io/trunk/RELEASE-NOTES.txt?rev=609253&r1=609252&r2=609253&view=diff
==============================================================================
--- commons/proper/io/trunk/RELEASE-NOTES.txt (original)
+++ commons/proper/io/trunk/RELEASE-NOTES.txt Sat Jan 5 18:28:01 2008
@@ -42,6 +42,8 @@
Enhancements from 1.3.2
-----------------------
+- FileUtils
+ - Add a deleteQuietly method [IO-135]
- TeeInputStream [IO-129]
- Add new Tee input stream implementation
Modified: commons/proper/io/trunk/src/java/org/apache/commons/io/FileUtils.java
URL:
http://svn.apache.org/viewvc/commons/proper/io/trunk/src/java/org/apache/commons/io/FileUtils.java?rev=609253&r1=609252&r2=609253&view=diff
==============================================================================
--- commons/proper/io/trunk/src/java/org/apache/commons/io/FileUtils.java
(original)
+++ commons/proper/io/trunk/src/java/org/apache/commons/io/FileUtils.java Sat
Jan 5 18:28:01 2008
@@ -893,6 +893,39 @@
}
/**
+ * 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.
*
* @param directory directory to clean
Modified:
commons/proper/io/trunk/src/test/org/apache/commons/io/FileUtilsTestCase.java
URL:
http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/org/apache/commons/io/FileUtilsTestCase.java?rev=609253&r1=609252&r2=609253&view=diff
==============================================================================
---
commons/proper/io/trunk/src/test/org/apache/commons/io/FileUtilsTestCase.java
(original)
+++
commons/proper/io/trunk/src/test/org/apache/commons/io/FileUtilsTestCase.java
Sat Jan 5 18:28:01 2008
@@ -1242,6 +1242,47 @@
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.
*/