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");
}
}
}
本文详细解析了Java中复制文件的copyFile方法,通过实例讲解其实现原理,包括checkFileRequirements方法的作用,帮助开发者更好地理解和运用。

235

被折叠的 条评论
为什么被折叠?



