import java.io.*;
import java.nio.channels.*;
/**
*
* @author 疏影暗香
*/
public class FileBack {
public static void copyDir(String srcDir, String desDir) throws IOException{
File src = new File(srcDir);
File[] fileList = src.listFiles();
for(int i=0;i<fileList.length;i++) {
if(fileList[i].isDirectory()) {
File subDir = new File(desDir+"/"+fileList[i].getName());
System.out.println(subDir.getAbsolutePath());
if(!subDir.exists()){
subDir.mkdir();
}
copyDir(srcDir + "/" + fileList[i].getName(), desDir+ "/" + fileList[i].getName());
}else {
File descDir = new File(desDir);
if (!descDir.exists()) {
descDir.mkdir();
}
copyFile(srcDir+"/" + fileList[i].getName(), desDir+ "/" + fileList[i].getName());
}
}
System.out.println("文件备份结束......");
}
public static void copyFile(String srcFile, String destFile) throws java.io.FileNotFoundException, java.io.IOException {
FileInputStream fis = new FileInputStream(srcFile);
FileOutputStream fos = new FileOutputStream(destFile);
FileChannel fcin = fis.getChannel();
FileChannel fcout = fos.getChannel();
fcin.transferTo(0, fcin.size(), fcout);
fcin.close();
fcout.close();
fis.close();
fos.close();
}
public static void main(String[] args) throws Exception{
System.out.print("请输入文件路径后回车:");
BufferedReader bin = new BufferedReader(new InputStreamReader(System.in));
String src = "";
src = bin.readLine();
String des = "f:/Inetpub";
File f = new File(des);
if(!(f.exists())) {
f.mkdir();
}
FileBack.copyDir(src, des);
}
}
3063

被折叠的 条评论
为什么被折叠?



