package ioTest;
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 Test4 {
public static void main(String[] args) {
//封装目录
File srcFolder = new File("C:\\changzidong");
//封装目的地
File destFolder = new File("C:\\test");
//如果目的地文件夹不存在,就创建
if (!destFolder.exists()) {
destFolder.mkdir();
}
//获取该目录下的所有文本的File数组
File[] fileArray = srcFolder.listFiles();
//遍历,并得到每一个File对象
for(File file : fileArray) {
String name = file.getName();
//destFolder是“C:\\test”,name是文件名,newFile对象就是完整的路径
File newFile = new File(destFolder,name);
copyFile(file,newFile );
//自定义方法
}
}
private static void copyFile(File file, File newFile) {
try {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(newFile));
byte []bys = new byte[1024];
int len = 0;
len = len + 0;
while((len = bis.read(bys)) != -1) {
bos.write(bys,0,len);
}
bis.close();
bos.close();
} catch (FileNotFoundException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
复制单级文件夹案例
最新推荐文章于 2021-03-05 09:10:05 发布