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

时间:2022-07-22
本文章向大家介绍字节流---复制文件和文件夹,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

复制文件

封装后的复制文件方法

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

完整方法:

	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类型

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

复制文件夹

封装后的复制文件方法

  1. 接收参数为两个File对象,代表输入和输出文件,并声明抛出IOException异常
public static void Copydirs(File src, File dest) throws IOException
  1. 判断是否为文件夹,如果是文件夹则在目标文件夹下建立源文件夹,调用复制文件夹
	public static void Copydirs(File src, File dest) throws IOException {
		if (src.isDirectory()) {
			dest = new File(dest, src.getName());
		}
		CopyDir(src, dest);
	}
  1. 判断是否为文件,如果是文件就直接复制,如果不是就建立文件夹然后再复制
	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类型

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

完整代码

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()));
			}
		}
	}
}