package com.copy;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* 拷贝文件夹
* @author cdhy
*
*/
public class CopyDir {
public static void main(String[] args) {
copyDir("D:\\phpStudy\\tools", "f:\\");
}
public static void copyDir(String from,String to){
File f = new File(from);
File t = new File(to+"/"+f.getName());
if(!t.exists())
t.mkdirs();
if(f.isDirectory()){
for (File file : f.listFiles()) {
if(file.isFile()){
copy(file.getPath(),to+"/"+f.getName()+"/"+file.getName());
}else{
copyDir(file.getPath(), to+"/"+f.getName());
}
}
}else{
new File(to).mkdirs();
copy(from,to+"/"+f.getName());
}
}
public static void copy(String from,String to){
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(from);
fos = new FileOutputStream(to);
byte [] bytes = new byte[1024];
int len = 0;
while((len = fis.read(bytes))!=-1){
fos.write(bytes,0,len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(fos !=null)
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
if(fis !=null)
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
拷贝文件夹复习
最新推荐文章于 2021-05-29 05:02:21 发布