首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >字节流---复制文件和文件夹

字节流---复制文件和文件夹

作者头像
shimeath
发布2020-07-30 18:40:10
发布2020-07-30 18:40:10
1.2K0
举报

复制文件

封装后的复制文件方法

  1. 接收参数为两个File对象,代表输入和输出文件,并声明抛出IOException异常
代码语言:javascript
复制
public static void CopyFile(File src, File dest) throws IOException;
  1. 判断是否为文件夹,如果是文件夹则抛出异常
代码语言:javascript
复制
		if (src.isDirectory()) {
			throw new IOException("这是一个文件夹");
		}
  1. 建立输入输出流
代码语言:javascript
复制
		InputStream iStream = new FileInputStream(src);
		OutputStream oStream = new FileOutputStream(dest);
  1. 创建字节数组为复制文件做准备,建立len整型变量记录长度
代码语言:javascript
复制
		byte[] flush = new byte[1024];
		int len = 0;
  1. 在未到达文件尾之前,读取文件并写入目标
代码语言:javascript
复制
		while (-1 != (len = iStream.read(flush))) {
			oStream.write(flush, 0, len);
		}
  1. 强制刷新输出流
代码语言:javascript
复制
		out.flush();
  1. 关闭输入输出流
代码语言:javascript
复制
		oStream.close();
		iStream.close();

完整方法:

代码语言:javascript
复制
	public static void CopyFile(File src, File dest) throws IOException {
		if (src.isDirectory()) {
			throw new IOException("这是一个文件夹");
		}
		InputStream iStream = new FileInputStream(src);
		OutputStream oStream = new FileOutputStream(dest);
		byte[] flush = new byte[1024];
		int len = 0;
		while (-1 != (len = iStream.read(flush))) {
			oStream.write(flush, 0, len);
		}
		oStream.flush();
		oStream.close();
		iStream.close();
	}

重载方法,使其可以接受String类型

代码语言:javascript
复制
	public static void CopyFile(String srcPath, String destPath) throws IOException {
		CopyFile(new File(srcPath), new File(destPath));
	}

复制文件夹

封装后的复制文件方法

  1. 接收参数为两个File对象,代表输入和输出文件,并声明抛出IOException异常
代码语言:javascript
复制
public static void Copydirs(File src, File dest) throws IOException
  1. 判断是否为文件夹,如果是文件夹则在目标文件夹下建立源文件夹,调用复制文件夹
代码语言:javascript
复制
	public static void Copydirs(File src, File dest) throws IOException {
		if (src.isDirectory()) {
			dest = new File(dest, src.getName());
		}
		CopyDir(src, dest);
	}
  1. 判断是否为文件,如果是文件就直接复制,如果不是就建立文件夹然后再复制
代码语言:javascript
复制
	private static void CopyDir(File src, File dest) throws IOException {
		if (src.isFile())
			CopyFile(src, dest); // 如果是文件就拷贝
		else if (src.isDirectory()) {
			dest.mkdirs();
			for (File sub : src.listFiles()) {
				CopyDir(sub, new File(dest, sub.getName()));
			}
		}
	}

重载方法,使其可以接受String类型

代码语言:javascript
复制
		public static void Copydirs(String srcPath, String destPath) throws IOException {
		Copydirs(new File(srcPath), new File(destPath));
	}

完整代码

代码语言:javascript
复制
package cn.hxh.io.stream;

import java.io.*;

public class CopyUtil {

	public static void main(String[] args) {

	}

	public static void CopyFile(String srcPath, String destPath) throws IOException {
		CopyFile(new File(srcPath), new File(destPath));
	}

	public static void CopyFile(File src, File dest) throws IOException {
		if (src.isDirectory()) {
			throw new IOException("这是一个文件夹");
		}
		InputStream iStream = new FileInputStream(src);
		OutputStream oStream = new FileOutputStream(dest);
		byte[] flush = new byte[1024];
		int len = 0;
		while (-1 != (len = iStream.read(flush))) {
			oStream.write(flush, 0, len);
		}
		oStream.flush();
		oStream.close();
		iStream.close();
	}

	public static void Copydirs(String srcPath, String destPath) throws IOException {
		Copydirs(new File(srcPath), new File(destPath));
	}

	public static void Copydirs(File src, File dest) throws IOException {
		if (src.isDirectory()) {
			dest = new File(dest, src.getName());
		}
		CopyDir(src, dest);
	}

	private static void CopyDir(File src, File dest) throws IOException {
		if (src.isFile())
			CopyFile(src, dest); // 如果是文件就拷贝
		else if (src.isDirectory()) {
			dest.mkdirs();
			for (File sub : src.listFiles()) {
				CopyDir(sub, new File(dest, sub.getName()));
			}
		}
	}
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-01-26 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 复制文件
    • 封装后的复制文件方法
      • 完整方法:
      • 重载方法,使其可以接受String类型
  • 复制文件夹
    • 封装后的复制文件方法
      • 重载方法,使其可以接受String类型
    • 完整代码
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档