* added copyTo() to scripting api

This commit is contained in:
Reinhard Pointner 2012-02-23 05:01:06 +00:00
parent 621e07c0f2
commit 6ac012ad5d
2 changed files with 26 additions and 0 deletions

View File

@ -38,6 +38,7 @@ File.metaClass.isDerived = { f -> isDerived(delegate, f) }
File.metaClass.validateFileName = { validateFileName(delegate) }
File.metaClass.validateFilePath = { validateFilePath(delegate) }
File.metaClass.moveTo = { f -> moveRename(delegate, f instanceof File ? f : new File(f.toString())) }
File.metaClass.copyTo = { dir -> copyAs(delegate, new File(dir, delegate.getName())) }
List.metaClass.mapByFolder = { mapByFolder(delegate) }
List.metaClass.mapByExtension = { mapByExtension(delegate) }
String.metaClass.getExtension = { getExtension(delegate) }

View File

@ -92,6 +92,31 @@ public final class FileUtilities {
}
public static File copyAs(File source, File destination) throws IOException {
// resolve destination
if (!destination.isAbsolute()) {
// same folder, different name
destination = new File(source.getParentFile(), destination.getPath());
}
// make sure we that we can create the destination folder structure
File destinationFolder = destination.getParentFile();
// create parent folder if necessary
if (!destinationFolder.isDirectory() && !destinationFolder.mkdirs()) {
throw new IOException("Failed to create folder: " + destinationFolder);
}
if (source.isDirectory()) { // copy folder
org.apache.commons.io.FileUtils.copyDirectory(source, destination);
} else { // copy file
org.apache.commons.io.FileUtils.copyFile(source, destination);
}
return destination;
}
public static byte[] readFile(File source) throws IOException {
InputStream in = new FileInputStream(source);