Copy directory
9
Methods for copy one directory (subdirectory inclusive).
Main method is xCopyDir.
Main method is xCopyDir.
public static void copy(File source, File dest) throws IOException {
FileChannel in = null, out = null;
try {
in = new FileInputStream(source).getChannel();
out = new FileOutputStream(dest).getChannel();
in.transferTo( 0, in.size(), out);
} finally {
if (in != null) {
in.close();
}
if (out != null){
out.close();
}
}
}
public static void xCopyDir(String sourceFolder, String destinyFolder)
throws IOException{
if (!new File(sourceFolder).isDirectory()){
throw new IOException("Source Directory error ");
}
boolean success = (new File(destinyFolder)).mkdirs();
if (!new File(destinyFolder).isDirectory()){
throw new IOException("Destini Directory error ");
}
copyRecursiveDir(sourceFolder, destinyFolder);
}
private static void copyRecursiveDir(String sourceFolder, String destinyFolder)
throws IOException{
File file;
String list[];
file = new File(sourceFolder);
if (file.isDirectory()) {
list = file.list();
for (int i = 0; i < list.length; i++){
if ( !new File(sourceFolder + File.separatorChar + list[i]).isFile()){
boolean success = (new File(destinyFolder +
File.separatorChar + list[i])).mkdirs();
}
copyRecursiveDir(sourceFolder + File.separatorChar + list[i],
destinyFolder + File.separatorChar + list[i]);
}
}
if(new File(sourceFolder).isFile()){
copy(new File(sourceFolder), new File(destinyFolder) );
}
}
Comments
Mon. Sep. 4th, 2006 1:58 PM
bertheymans
bertheymans





