/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package maintenance;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
*
* @author ryan.o.salvador
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
// TODO code application logic here
String src = "D:\\try";
File source = new File(src);
if(!source.exists()){
System.out.println("File or directory does not exist.");
System.exit(0);
}
String dest = "C:\\try";
File destination = new File(dest);
if(!destination.exists()){
System.out.println("destination dir doesnt exists");
destination.mkdir();
}
copyDirectory(source, destination);
}
private static void copyDirectory(File sourceDir, File destDir) throws IOException{
System.out.println("Copying directory");
File[] children = sourceDir.listFiles();
System.out.println(destDir +" exists:"+destDir.exists());
if(!destDir.exists()){
destDir.mkdir();
System.out.println("destination dir:"+destDir);
}
for(File sourceChild:children){
//System.out.println(sourceChild.getName());
String name = sourceChild.getName();
System.out.println("Filename:"+name);
File destChild = new File(destDir,name);
if(sourceChild.isDirectory()){
System.out.println("test sourceChild"+sourceChild.isDirectory());
copyDirectory(sourceChild, destChild);
}
else{
copyFile(sourceChild, destChild);
}
}
}
private static void copyFile(File source, File dest) throws IOException{
if(!dest.exists()){
System.out.println("dest:"+dest);
dest.createNewFile();
}
System.out.println("Writing file :"+source.getPath()+" to: "+dest.getPath());
InputStream in = null;
OutputStream out = null;
try{
in = new FileInputStream(source);
out = new FileOutputStream(dest);
byte[] buf = new byte[1024];
int len;
while((len=in.read(buf))>0){
out.write(buf, 0, len);
}
}
finally{
in.close();
out.close();
}
}
}