IO学习(六)拷贝文件夹

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


模拟实例:将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();
	}
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值