Java File类中常见操作之文件夹的复制
public class CopyDir {
public static void main(String[] args) throws IOException {
File srcfile=new File("E:/桌面/WorkSpace");
File desfile=new File("E:/桌面/123");
copyDir(srcfile,desfile);
}
//复制文件夹
public static void copyDir(File srcfile,File desfile) throws IOException{
if(!desfile.exists()){
desfile.mkdirs();
}
File[] files = srcfile.listFiles();
for(File file:files){
if(file.isFile()){
copyFile(file,new File(desfile,file.getName()));
}else if(file.isDirectory()){
copyDir(file,new File(desfile,file.getName()));
}
}
}
//文件的复制
public static void copyFile(File srcfile,File desfile) throws IOException{
//读写文件
FileInputStream fis=new FileInputStream(srcfile);
FileOutputStream fos=new FileOutputStream(desfile);
int len;
byte[] bys=new byte[1024];
while((len=fis.read(bys))!=-1){
fos.write(bys, 0, len);
fos.flush();
}
fos.close();
fis.close();
}
}