Java_IO_复制文件到文件copyFile实现方法

本文详细解析了Java中复制文件的copyFile方法,通过实例讲解其实现原理,包括checkFileRequirements方法的作用,帮助开发者更好地理解和运用。
package cn.CommonsIO;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class CopyFileDemo {
	public static void main(String[] args) throws IOException {
		copyFile(new File("abc.txt"), new File("abc2.txt"));
	}

	/**
	 * 复制文件到文件(可选是否改变文件最后一次修改时间)
	 * @param srcFile          
	 * @param destFile         
	 * @param preserveFileDate 
	 */
	public static void copyFile(File srcFile, File destFile, boolean preserveFileDate) throws IOException {
//		检查源与目标是否满足基本条件
		CheckFileRequirements.checkFileRequirements(srcFile, destFile);
//		异常:源是一个文件夹
		if (srcFile.isDirectory()) {
			throw new IOException("源" + srcFile + "是一个文件夹");
		}
//		异常:源与目标相同
		if (srcFile.getCanonicalPath().equals(destFile.getCanonicalPath())) {
			throw new IOException("源" + srcFile + "与目标" + destFile + "相同");
		}

		File parentFile = destFile.getParentFile();
//		异常:目标文件父目录不能被创建
		if (parentFile != null) {
			if (!parentFile.mkdirs() && !parentFile.isDirectory()) {
				throw new IOException("目标父目录" + parentFile + "不能被创建");
			}
		}
//		异常:目标文件只读
		if (destFile.exists() && destFile.canWrite() == false) {
			throw new IOException("目标文件" + destFile + "只读");
		}
//		异常:目标文件是一个文件夹
		if (destFile.exists() && destFile.isDirectory()) {
			throw new IOException("目标" + destFile + "存在但是一个文件夹");
		}
//		异常检查结束,执行文件复制方法
		doCopyFile(srcFile, destFile, preserveFileDate);
	}

	/**
	 * 内部复制文件方法
	 * @param srcFile          
	 * @param destFile         
	 * @param preserveFileDate 
	 */
	private static void doCopyFile(File srcFile, File destFile, boolean preserveFileDate) throws IOException {
//		此处与源码不同
		try (InputStream is = new BufferedInputStream(new FileInputStream(srcFile));
				OutputStream os = new BufferedOutputStream(new FileOutputStream(destFile));) {
			byte[] flush = new byte[1024];
			int len = -1;
			while ((len = is.read(flush)) != -1) {
				os.write(flush, 0, len);
			}
			os.flush();
		}

//		异常:未完全复制
		long srcLen = srcFile.length();
		long dstLen = destFile.length();
		if (srcLen != dstLen) {
			throw new IOException("未能够将全部内容从" + srcFile + "复制到" + destFile + "预计大小: " + srcLen + "实际拷贝: " + dstLen);
		}

//		保留最后一次修改时间方法
		if (preserveFileDate) {
			destFile.setLastModified(srcFile.lastModified());
		}
	}

	/**
	 * 方法重载:复制文件到文件(不改变修改文件最后一次修改时间)
	 * @param srcFile  
	 * @param destFile 
	 */
	public static void copyFile(final File srcFile, final File destFile) throws IOException {
		copyFile(srcFile, destFile, true);
	}
}

以上代码中出现的checkFileRequirements方法

package cn.CommonsIO;

import java.io.File;
import java.io.FileNotFoundException;

public class CheckFileRequirements {
	/**
	 * 检查复制文件是否符合基本条件
	 * @param src 
	 * @param dest 
	 */
	public static void checkFileRequirements(final File src, final File dest) throws FileNotFoundException {
		if (src == null) {
			throw new NullPointerException("Source must not be null");
		}
		if (dest == null) {
			throw new NullPointerException("Destination must not be null");
		}
		if (!src.exists()) {
			throw new FileNotFoundException("Source '" + src + "' does not exist");
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值