【Java】复制单级文件夹
package LearnJava15;
import java.io.*;
public class CopyFolderDemo {
public static void main(String[] args) throws IOException {
// 创建数据源目录对象
File srcFolder =new File("D:\\MyProject\\Java\\Demo2\\test\\");
// 获取数据源目录对象的名称
String srcFileName = srcFolder.getName();
// 创建目的地目录对象
File destFolder=new File("myCharSTream",srcFileName);
// 判断目的地目录对应的File是否存在,如果不存在就创建
if (!destFolder.exists()){
destFolder.mkdir();
}
// 获取数据源目录下所有文件的File数组
File[] listfiles = srcFolder.listFiles();
// 遍历File数组,得到每一个File对象
for (File srcFile:listfiles){
srcFileName = srcFolder.getName();
File destFile=new File(destFolder,srcFileName);
// 复制文件
copyFile(srcFile,destFile);
}
}
private static void copyFile(File srcFile, File destFile) throws IOException {
BufferedInputStream bis=new BufferedInputStream(new FileInputStream(srcFile));
BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(destFile));
byte[] bys=new byte[1024];
int len;
while ((len=bis.read(bys))!=-1){
bos.write(bys,0,len);
}
bos.close();
bis.close();
}
}
本文来自博客园,作者:木子欢儿,转载请注明原文链接:https://www.cnblogs.com/HGNET/p/16196103.html