You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@avalon.apache.org by do...@apache.org on 2002/05/30 14:23:52 UTC
cvs commit: jakarta-avalon-excalibur/io/src/java/org/apache/avalon/excalibur/io FileUtil.java
donaldp 02/05/30 05:23:52
Modified: io/src/java/org/apache/avalon/excalibur/io FileUtil.java
Log:
Add a utility method to schedule deletes of files/directorys
Revision Changes Path
1.29 +94 -6 jakarta-avalon-excalibur/io/src/java/org/apache/avalon/excalibur/io/FileUtil.java
Index: FileUtil.java
===================================================================
RCS file: /home/cvs/jakarta-avalon-excalibur/io/src/java/org/apache/avalon/excalibur/io/FileUtil.java,v
retrieving revision 1.28
retrieving revision 1.29
diff -u -r1.28 -r1.29
--- FileUtil.java 10 May 2002 02:51:14 -0000 1.28
+++ FileUtil.java 30 May 2002 12:23:52 -0000 1.29
@@ -44,7 +44,7 @@
*
* @author <a href="mailto:peter@apache.org">Peter Donald</a>
* @author <a href="mailto:jefft@apache.org">Jeff Turner</a>
- * @version CVS $Revision: 1.28 $ $Date: 2002/05/10 02:51:14 $
+ * @version CVS $Revision: 1.29 $ $Date: 2002/05/30 12:23:52 $
* @since 4.0
*/
public final class FileUtil
@@ -633,13 +633,87 @@
{
if( !file.delete() )
{
- final String message = "File " + file + " unable to be deleted.";
+ final String message =
+ "File " + file + " unable to be deleted.";
throw new IOException( message );
}
}
}
/**
+ * Schedule a file to be deleted when JVM exits.
+ * If file is directory delete it and all sub-directories.
+ */
+ public static void forceDeleteOnExit( final File file )
+ throws IOException
+ {
+ if( file.isDirectory() )
+ {
+ deleteDirectoryOnExit( file );
+ }
+ else
+ {
+ file.deleteOnExit();
+ }
+ }
+
+ /**
+ * Recursively schedule directory for deletion on JVM exit.
+ */
+ private static void deleteDirectoryOnExit( final File directory )
+ throws IOException
+ {
+ if( !directory.exists() )
+ {
+ return;
+ }
+
+ cleanDirectoryOnExit( directory );
+ directory.deleteOnExit();
+ }
+
+ /**
+ * Clean a directory without deleting it.
+ */
+ private static void cleanDirectoryOnExit( final File directory )
+ throws IOException
+ {
+ if( !directory.exists() )
+ {
+ final String message = directory + " does not exist";
+ throw new IllegalArgumentException( message );
+ }
+
+ if( !directory.isDirectory() )
+ {
+ final String message = directory + " is not a directory";
+ throw new IllegalArgumentException( message );
+ }
+
+ IOException exception = null;
+
+ final File[] files = directory.listFiles();
+ for( int i = 0; i < files.length; i++ )
+ {
+ final File file = files[ i ];
+ try
+ {
+ FileUtil.forceDeleteOnExit( file );
+ }
+ catch( final IOException ioe )
+ {
+ exception = ioe;
+ }
+ }
+
+ if( null != exception )
+ {
+ throw exception;
+ }
+ }
+
+
+ /**
* Make a directory. If there already exists a file with specified name or
* the directory is unable to be created then an exception is thrown.
*/
@@ -686,9 +760,11 @@
}
cleanDirectory( directory );
- if( false == directory.delete() )
+ if( !directory.delete() )
{
- throw new IOException( "Directory " + directory + " unable to be deleted." );
+ final String message =
+ "Directory " + directory + " unable to be deleted.";
+ throw new IOException( message );
}
}
@@ -719,13 +795,25 @@
throw new IllegalArgumentException( message );
}
- final File[] files = directory.listFiles();
+ IOException exception = null;
+ final File[] files = directory.listFiles();
for( int i = 0; i < files.length; i++ )
{
final File file = files[ i ];
+ try
+ {
+ FileUtil.forceDelete( file );
+ }
+ catch( final IOException ioe )
+ {
+ exception = ioe;
+ }
+ }
- FileUtil.forceDelete( file );
+ if( null != exception )
+ {
+ throw exception;
}
}
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>