package com.px.test;
import java.io.*;
public class copyFile {
//文件拷贝
public void fileCopy(File file,String descDir){
if(file.isFile()){
copy(file,descDir);//如果是文件直接拷贝
}else{
//如果是目录 新建一个目录
String s=file.getName();//新建的目录名称
String ss=descDir+File.separator+s;
File f=new File(ss);
f.mkdirs();//创建了目录 遍历
File[] f2=file.listFiles();
for(File ff:f2){
if(ff.isFile()){
//String s1=ff.getName();
//String s2=ss+s1;
copy(ff,ss);
}else{
fileCopy(ff,ss);
}
}
}
}
public void copy(File file,String desc){
String name=file.getName();
String dir=desc+File.separator+name;
InputStream in=null;
OutputStream out=null;
try {
in=new FileInputStream(file);
out=new FileOutputStream(dir);
int i=0;
byte [] b=new byte[1024*8];
while((i=in.read(b))!=-1){
out.write(b, 0, i);
System.out.println("正在拷贝!!!");
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(in!=null){
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}if(out!=null){
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
copyFile cf=new copyFile();
File file=new File("C:\\Program Files\\Java\\jdk1.6.0_02\\src");
cf.fileCopy(file,"C:");
System.out.println("拷贝完成");
}
}
java文件夹的拷贝
最新推荐文章于 2023-06-03 18:39:03 发布