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

复制文件

封装后的复制文件方法

  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()));
			}
		}
	}
}
好的,下面是 Java 字节流实现整个文件夹复制的代码示例: ```java import java.io.*; public class FolderCopy { public static void main(String[] args) throws IOException { File sourceFolder = new File("sourceFolder"); // 源文件夹路径 File targetFolder = new File("targetFolder"); // 目标文件夹路径 if (!targetFolder.exists()) { targetFolder.mkdir(); // 如果目标文件夹不存在,创建目标文件夹 } File[] files = sourceFolder.listFiles(); // 获取源文件夹下的所有文件文件夹 for (File file : files) { if (file.isFile()) { // 如果是文件,直接复制 FileInputStream in = new FileInputStream(file); FileOutputStream out = new FileOutputStream(targetFolder + "\\" + file.getName()); byte[] buffer = new byte[1024]; int bytesRead; while ((bytesRead = in.read(buffer)) != -1) { out.write(buffer, 0, bytesRead); } in.close(); out.close(); } else if (file.isDirectory()) { // 如果是文件夹,递归调用复制方法 File newFolder = new File(targetFolder + "\\" + file.getName()); newFolder.mkdir(); FolderCopy.copyFolder(file, newFolder); } } System.out.println("文件夹复制成功!"); } public static void copyFolder(File sourceFolder, File targetFolder) throws IOException { if (!targetFolder.exists()) { targetFolder.mkdir(); // 如果目标文件夹不存在,创建目标文件夹 } File[] files = sourceFolder.listFiles(); // 获取源文件夹下的所有文件文件夹 for (File file : files) { if (file.isFile()) { // 如果是文件,直接复制 FileInputStream in = new FileInputStream(file); FileOutputStream out = new FileOutputStream(targetFolder + "\\" + file.getName()); byte[] buffer = new byte[1024]; int bytesRead; while ((bytesRead = in.read(buffer)) != -1) { out.write(buffer, 0, bytesRead); } in.close(); out.close(); } else if (file.isDirectory()) { // 如果是文件夹,递归调用复制方法 File newFolder = new File(targetFolder + "\\" + file.getName()); newFolder.mkdir(); FolderCopy.copyFolder(file, newFolder); } } } } ``` 其中,`File` 代表文件文件夹,`File.listFiles()` 方法返回该文件夹下所有文件文件夹的数组。我们通过遍历数组,如果发现是文件直接复制,如果发现是文件夹则递归调用自己的复制方法。在复制文件时,我们使用相同的字节流复制文件。最后,我们输出复制成功的提示信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值