IO学习(六)拷贝文件夹

时间:2022-06-22 00:27:10

文件夹用来把文件包裹起来,褪去这些外衣,说到底拷贝文件夹也就是拷贝文件


模拟实例:将F:/Picture/test 文件夹 拷贝到 F:/Picture/dir文件夹

该实例中test文件夹下只包含了test.txt文件


步骤分析:

1.通过路径得到File对象

2.递归查找子孙级文件夹或者文件

3.复制文件(同文件拷贝)

那么重点是在第二个步骤,我们可以通过File对象的listFiles方法得到目标文件夹下所包括的文件,listFiles方法返回一个泛型为File的集合list,由此我们就得到了test文件夹下所有的文件,通过foreach循环语句遍历这个list,得到的每一个File对象,首先要做的就是判断这个File对象是文件还是文件夹,如果是文件就可直接copy,如果是文件夹,则需要再通过listFiles方法得到目标文件夹下所包括的文件,步骤与上面一致,这也就是递归的思想

需要注意的一点是,我们需要把整个test文件夹拷贝到dir文件夹,那么当遍历到test文件夹下的test.txt文件时,我们在拷贝的时候,需要重新创建一个新的目标文件,dir/test/text.txt.,这就需要File的另一个构造方法

File(File parent,
String child)

根据 parent 抽象路径名和 child 路径名字符串创建一个新 File 实例

在得到dir这个文件夹的时候,也应该用上述构造方法,得到dir/testFile新对象


在拷贝文件的时候,使用了不同的流,
之前拷贝文件使用的FileInputStream与FileOutputStream,

这里使用了BufferedInputStream与BufferedOutputStream,使用方法相似

InputStream is =new BufferedInputStream(new FileInputStream(src));
OutputStream os =new BufferedOutputStream(new FileOutputStream(dest));


代码:

public class Demo03 {
/**
* @param args
*/
public static void main(String[] args) {
// 源目录
String srcPath = "F:/Picture/test";

// 目标目录
String destPath = "F:/Picture/dir";

//进行拷贝
try {
copyDir(srcPath, destPath);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

/**
* 通过路径获得File对象
*
* @param src源路径
* @param dest目标路径
* @throws IOException
* @throws FileNotFoundException
*/
public static void copyDir(String srcPath,String destPath) throws FileNotFoundException, IOException{
//拒绝自己拷贝给自己
if(srcPath.equals(destPath)){
return ;
}
File src=new File(srcPath);
File dest =new File(destPath);
copyDir(src,dest);
}



/**
* 拷贝文件夹
* @param src 源File对象
* @param dest 目标File对象
* @throws IOException
* @throws FileNotFoundException
*/
public static void copyDir(File src,File dest) throws FileNotFoundException, IOException{
if(src.isDirectory()){ //文件夹
dest =new File(dest,src.getName());
if(dest.getAbsolutePath().contains(src.getAbsolutePath())){
System.out.println("父目录不能拷贝到子目录中");
return;
}
}
copyDirDetail(src,dest);
}

/**
* 拷贝文件夹细节
* @param src
* @param dest
*/
public static void copyDirDetail(File src,File dest) throws FileNotFoundException,IOException{
if(src.isFile()){ //文件
copyFile(src, dest);
}else if(src.isDirectory()){ //文件夹
//确保目标文件夹存在
dest.mkdirs();
//获取下一级目录|文件
for(File sub:src.listFiles()){
copyDirDetail(sub,new File(dest,sub.getName()));
}
}
}


/**
* 文件的拷贝,得到File对象
* @param 源文件路径
* @param 目录文件路径
* @throws FileNotFoundException,IOException
* @return
*/
public static void copyFile(String srcPath,String destPath) throws FileNotFoundException,IOException {
//1、建立联系 源(存在且为文件) +目的地(文件可以不存在)
copyFile(new File(srcPath),new File(destPath));
}
/**
* 文件的拷贝
* @param 源文件File对象
* @param 目录文件File对象
* @throws FileNotFoundException,IOException
* @return
*/
public static void copyFile(File src,File dest) throws FileNotFoundException,IOException {
if(! src.isFile()){ //不是文件或者为null
System.out.println("只能拷贝文件");
throw new IOException("只能拷贝文件");
}
//dest为已经存在的文件夹,不能建立于文件夹同名的文件
if(dest.isDirectory()){
System.out.println(dest.getAbsolutePath()+"不能建立于文件夹同名的文件");
throw new IOException(dest.getAbsolutePath()+"不能建立于文件夹同名的文件");
}

//2、选择流
InputStream is =new BufferedInputStream(new FileInputStream(src));
OutputStream os =new BufferedOutputStream(new FileOutputStream(dest));

//3、文件拷贝 循环+读取+写出
byte[] flush =new byte[1024];
int len =0;

//读取
while(-1!=(len=is.read(flush))){
//写出
os.write(flush, 0, len);
}
os.flush(); //强制刷出

//关闭流
os.close();
is.close();
}
}