* use JDK7 Files.copy() if possible

This commit is contained in:
Reinhard Pointner 2012-07-16 19:04:45 +00:00
parent 5cb775fdac
commit 45594c4179
1 changed files with 16 additions and 25 deletions

View File

@ -46,13 +46,15 @@ public final class FileUtilities {
// resolve destination
destination = resolveDestination(source, destination);
if (source.isDirectory()) { // move folder
moveFolderIO(source, destination);
} else { // move file
if (source.isDirectory()) {
// move folder
org.apache.commons.io.FileUtils.moveDirectory(source, destination);
} else {
// move file
try {
moveFileNIO2(source, destination);
java.nio.file.Files.move(source.toPath(), destination.toPath());
} catch (LinkageError e) {
moveFileIO(source, destination);
org.apache.commons.io.FileUtils.moveFile(source, destination); // use "copy and delete" as fallback if standard rename fails
}
}
@ -60,32 +62,21 @@ public final class FileUtilities {
}
private static void moveFileNIO2(File source, File destination) throws IOException {
java.nio.file.Files.move(source.toPath(), destination.toPath());
}
private static void moveFileIO(File source, File destination) throws IOException {
// use "copy and delete" as fallback if standard rename fails
org.apache.commons.io.FileUtils.moveFile(source, destination);
}
private static void moveFolderIO(File source, File destination) throws IOException {
// use "copy and delete" as fallback if standard move/rename fails
org.apache.commons.io.FileUtils.moveDirectory(source, destination);
}
public static File copyAs(File source, File destination) throws IOException {
// resolve destination
destination = resolveDestination(source, destination);
if (source.isDirectory()) { // copy folder
if (source.isDirectory()) {
// copy folder
org.apache.commons.io.FileUtils.copyDirectory(source, destination);
} else { // copy file
} else {
// copy file
try {
java.nio.file.Files.copy(source.toPath(), destination.toPath());
} catch (LinkageError e) {
org.apache.commons.io.FileUtils.copyFile(source, destination);
}
}
return destination;
}