一、Create、Modify
1、copyFileToDirectory
/**
* Copies a file to a directory optionallypreserving the file date.
* <p>
* This method copies the contents of thespecified source file
* toa file of the same name in the specified destination directory.
// destDir必须是文件夹,若不存在则会自动创建
* The destination directory is created ifit does not exist.
* If the destination file exists, thenthis method will overwrite it.
*/
publicstaticvoidcopyFileToDirectory(final File srcFile, final File destDir, finalbooleanpreserveFileDate)
throws IOException {
if (destDir == null) {
thrownew NullPointerException("Destination must not be null");
}
if (destDir.exists()&& destDir.isDirectory() == false) {
thrownew IllegalArgumentException("Destination '" + destDir + "' is not a directory");
}
final File destFile = new File(destDir, srcFile.getName());
//java.io.File.File(File parent,String child)
copyFile(srcFile, destFile, preserveFileDate);
//内部调用下面的copyFile方法
}
/**
* Copies a file to a new location.
* <p>
* This method copies the contents of thespecified source file
* to the specified destination file.
* The directory holding the destinationfile is created if it does not exist. // 目标文件父目录不存在则会创建
* If the destination file exists, thenthis method will overwrite it.
* // 目标文件存在则会覆盖
*/
publicstaticvoid copyFile(final File srcFile, final File destFile,
finalbooleanpreserveFileDate) throws IOException {
checkFileRequirements(srcFile, destFile);
if (srcFile.isDirectory()) {
thrownew IOException("Source'" + srcFile + "' exists but is a directory");
}
if (srcFile.getCanonicalPath().equals(destFile.getCanonicalPath())){
thrownew IOException("Source'" + srcFile + "' and destination '" + destFile + "' arethe same");
}
final File parentFile = destFile.getParentFile();
if (parentFile != null) {
if (!parentFile.mkdirs() && !parentFile.isDirectory()){
thrownew IOException("Destination'" + parentFile + "' directory cannot be created");
}
}
if (destFile.exists() && destFile.canWrite() == false) {
thrownew IOException("Destination'" + destFile + "' exists but is read-only");
}
doCopyFile (srcFile, destFile, preserveFileDate);
// 内部调用doCopyFile方法实现
}
}
/**
* Internal copy file method.
* This caches the original file length,and throws an IOException
* if the output file length is differentfrom the current input file length. // 传输过程中文件改变会抛异常
* So it may fail if the file changes size.
* It may also fail with"IllegalArgumentException: Negative size" if the input file istruncated part way
* through copying the data and the newfile size is less than the current position.
*/
privatestaticvoid doCopyFile(final File srcFile, final File destFile, finalbooleanpreserveFileDate)
throws IOException {
if (destFile.exists() && destFile.isDirectory()){
thrownew IOException("Destination'" + destFile + "' exists but is a directory");
}
try (FileInputStream fis = new FileInputStream(srcFile);
FileChannel input = fis.getChannel();
// 内部调用NIO 的channel
FileOutputStream fos = new FileOutputStream(destFile);
FileChannel output = fos.getChannel()) {