import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class CopyFile {
public static void main(String[] args) throws IOException {
//封装数据源
File srefile = new File("d:\\copy");
//判断是否存在文件夹
if (!srefile.exists()){
System.out.println("数据源有问题");
System.exit(0);
}
File[] fileArray = srefile.listFiles();
//封装目的地
File destfile = new File("d:\\");
if(!destfile.exists()){
destfile.mkdirs();
}
for(File file : fileArray){
String name = file.getName();
File tfile = new File(destfile, name);
copyfile(file, tfile);
}
}
public static void copyfile(File file, File tfile) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(tfile));
byte[] by = new byte[1024];
int len = 0;
while ((len = bis.read(by)) != -1){
bos.write(by, 0, len);
}
bos.close();
bis.close();
}
}
希望对L、Z有帮助 |