Migrated Multiverse-Adventure's copyFolder() into core.

This commit is contained in:
Jeremy Wood 2012-07-19 21:08:39 -04:00
parent 3e7841afc9
commit 085c7a1ccc

View File

@ -8,6 +8,13 @@
package com.onarandombox.MultiverseCore.utils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.logging.Logger;
/**
* File-utilities.
@ -37,4 +44,57 @@ public class FileUtils {
return false;
}
}
/**
* Helper method to copy the world-folder
*
* @returns if it had success
*/
public static boolean copyFolder(File source, File target, Logger log) {
InputStream in = null;
OutputStream out = null;
try {
if (source.isDirectory()) {
if (!target.exists())
target.mkdir();
String[] children = source.list();
// for (int i=0; i<children.length; i++) {
for (String child : children) {
copyFolder(new File(source, child), new File(target, child), log);
}
}
else {
in = new FileInputStream(source);
out = new FileOutputStream(target);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
}
return true;
}
catch (FileNotFoundException e) {
log.warning("Exception while copying file: " + e.getMessage());
}
catch (IOException e) {
log.warning("Exception while copying file: " + e.getMessage());
}
finally {
if (in != null) {
try {
in.close();
} catch (IOException ignore) { }
}
if (out != null) {
try {
out.close();
} catch (IOException ignore) { }
}
}
return false;
}
}