Java Reference
In-Depth Information
String . format ( "Destination dir %s must exist" , toDir ));
}
for
for ( File src : fromDir . listFiles ()) {
iif ( src . isDirectory ()) {
File destSubDir = new
new File ( toDir , src . getName ());
copyRecursively ( src , destSubDir , true
true );
} else
else iif ( src . isFile ()) {
copyFile ( src , toDir );
} else
else {
System . err . println (
String . format ( "Warning: %s is neither file nor directory" , src ));
}
}
}
public
public static
static void
void copyRecursively ( File fromDir , File toDir ) throws
throws IOException {
copyRecursively ( fromDir , toDir , false
false );
}
public
public static
static void
void deleteRecursively ( File startDir ) throws
throws IOException {
String startDirPath = startDir . getCanonicalPath ();
// Pass one - delete recursively
for
for ( File f : startDir . listFiles ()) {
iif (! f . getCanonicalPath (). startsWith ( startDirPath )) {
throw
throw new
new IOException ( "Attempted to go out of " + startDir );
}
iif ( f . isDirectory ()) {
deleteRecursively ( f );
}
}
// Pass two - delete whatever's left: files and (empty) directories
for
for ( File f : startDir . listFiles ()) {
f . delete ();
iif ( f . exists ()) {
System . err . println ( f + " did not get deleted!" );
}
}
// Pass three - delete the (now empty) starting directory
startDir . delete ();
}
/**
* Copy a tree of files to directory, given File objects representing the files.
Search WWH ::




Custom Search