* added copyTo() to scripting api
This commit is contained in:
parent
621e07c0f2
commit
6ac012ad5d
|
@ -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) }
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
Loading…
Reference in New Issue