package Demo;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class CopyDir {
private String oldFile,newFile;
CopyDir(String oldFile,String newFile){
this.oldFile = oldFile;
this.newFile = newFile;
}
//在f盘创建母文件夹
void copyDir() throws IOException{
File nFile = new File(newFile);
nFile.mkdir();
listSuper(oldFile);
}
//遍历母文件夹
void listSuper(String oldFile) throws IOException{
File a = new File(oldFile);
File[] file = a.listFiles();
for(int i = 0;i<file.length;i++){
System.out.println(file);
if(file.isDirectory()){
new File(oldFile+"\\"+(file.getName().toString())).mkdir();
oldFile = oldFile+"\\"+file.getName().toString();
listSuper(oldFile);
}else{
createSonFile(file);
}
}
}
//复制子文件
private void createSonFile(File file) throws IOException {
FileInputStream in = new FileInputStream(oldFile+"\\"+(file.getName().toString()));
FileOutputStream out = new FileOutputStream(newFile+"\\"+(file.getName().toString()));
int n = 0;
byte[] bb = new byte[1024*5];
while ((n = in.read(bb)) != -1) {
out.write(bb, 0, n);
}
out.close();
in.close();
}
public static void main(String[] args) throws IOException{
new CopyDir("H:\\养生功","F:\\养生功").copyDir();
}
}
我就那我家电脑上的一个养生操文件夹和里面所有的子文件夹和文件作为复制源,运行总是说找不到指定文件,我都无语了,看半天没看出来哪里有问题,请大神帮忙纠错~ |
|