方法摘要:
copyFilesMain() 调用复制文件夹方法示例
copyFileMain() 调用复制单文件方法示例
copyFile() 复制单文件方法
copyFiles() 复制文件夹方法
说明:源代码可全部直接Copy运行。
源代码:
package com.file;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
/**
* 复制文件
* @author by 尊重审查
* 2017-5-11 <br> 下午1:33:16
*/
public class CopyFile {
static int errorFileCount = 0; // 记录失败的文件数
static int hiddenFileCount = 0; // 记录隐藏的文件数
static int successFileCount = 0; // 记录成功的文件数
static int successFilesCount = 0;// 记录成功的文件夹
static StringBuffer errorMsg = new StringBuffer(); // 记录异常信息
public static void main(String[] args) {
try {
/* 复制单个文件 */
// copyFileMain();
/* 复制一个文件夹 */
long startTime = System.currentTimeMillis(); // 获取开始时间
copyFilesMain();
long endTime = System.currentTimeMillis(); // 获取结束时间
System.out.println("耗时:" + (endTime - startTime) + "ms"); // 打印程序运行时间,1000ms = 1s;
System.exit(0);
}
catch (Exception e) {
e.printStackTrace();
}
}
/**
* 复制文件夹调用示例
*
* by 尊重审查 <br> 2017-5-16 <br> 上午9:41:07
*/
static void copyFilesMain() {
String oldFilesPath = "F:/OwO/CMMI3";
String newFilesPath = "F:/OwO/testFiles/CMMI3-Copy";
System.out.println("从 " + oldFilesPath + " 复制到 " + newFilesPath );
new File(newFilesPath).mkdirs(); // 首先创建根目录文件夹
copyFiles(oldFilesPath,newFilesPath);
System.out.println("复制完成。");
printMsg();
}
/**
* 复制单文件调用示例
*
* by 尊重审查 <br> 2017-5-16 <br> 上午9:41:29
*/
static void copyFileMain() {
try {
String oldFilePath = "F:/OwO/testFiles/update.txt";
String newFilePath = "F:/OwO/testFiles/update-copy.txt";
copyFile(oldFilePath , newFilePath);
}
catch (Exception e) {
e.printStackTrace();
}
}
/**
* 复制单个文件
* by 尊重审查 <br> 2017-5-15 <br> 下午4:41:31
* @param oldFilePath 需要复制的文件路径
* @param newFilePath 复制之后的文件路径
* @throws Exception
*/
static void copyFile( String oldFilePath, String newFilePath ) throws Exception{
FileChannel in = null;
FileChannel out = null;
FileInputStream input = null;
FileOutputStream output = null;
try {
input = new FileInputStream(oldFilePath);
output = new FileOutputStream(newFilePath);
in = input.getChannel();
out = output.getChannel();
in.transferTo(0, in.size(), out);
System.out.println("复制" + newFilePath + " >>> SUCCESS ");
}
catch (Exception e) {
errorFileCount++ ; // 记录失败的文件数量
throw e;
}
finally {
try {
input.close();
in.close();
output.close();
out.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 复制文件夹
* by 尊重审查 <br> 2017-5-15 <br> 下午4:43:10
* @param oldFilesPath 需要复制的文件夹路径
* @param newFilesPath 复制之后的文件夹路径
*/
static void copyFiles( String oldFilesPath, String newFilesPath ) {
/*
* 关键方法摘要:
* 0. File.listFiles() 获取该路径下的所有文件,返回 File[] 数组。
* 1. File.isDirectory() 判断是否为文件夹。
* 2. File.isFile() 判断是否为文件。
*
* 思路:
* 使用递归。
*
*/
try {
/* 变量区 */
String oldCopyFilePath ; // 需要复制的文件夹路径
String newCopyFilePath ; // 复制之后的文件夹路径
/* 获取需要复制的文件夹下的文件数组 */
File file = new File(oldFilesPath);
File[] listFiles = file.listFiles();
/* 循环文件夹数组 */
for (int i = 0; i < listFiles.length; i++) {
// 文件
if (listFiles[i].isFile()) {
try {
oldCopyFilePath = oldFilesPath + "/" + listFiles[i].getName(); // 需要复制的文件夹路径 + 当前文件名
newCopyFilePath = newFilesPath + "/" + listFiles[i].getName(); // 复制之后的文件夹路径 + 当前文件名
copyFile(oldCopyFilePath,newCopyFilePath); // 复制文件
successFileCount++; // 记录文件的成功总数
}
catch (Exception e) {
errorMsg.append(e.toString()); // 记录异常信息
}
}
// 文件夹
else if (listFiles[i].isDirectory()) {
try {
oldCopyFilePath = oldFilesPath + "/" + listFiles[i].getName();
newCopyFilePath = newFilesPath + "/" + listFiles[i].getName();
new File(newCopyFilePath).mkdirs(); // 创建文件夹
copyFiles(oldCopyFilePath,newCopyFilePath); // 注意这里调用的方法是加了 s 的。
successFilesCount++; // 记录文件的成功总数
}
catch (Exception e) {
errorMsg.append(e.toString()); // 记录异常信息
}
}
// 隐藏文件
else if( listFiles[i].isHidden() ){
hiddenFileCount++; // 记录隐藏的文件数
}
// else
else {
System.err.println("发现未知文件类型,跳过。");
}
}
}
catch (Exception e) {
e.printStackTrace();
}
}
static void printMsg(){
System.out.println(" <-----------------------------------------------------------> ");
System.out.println(" 隐藏文件:" + hiddenFileCount);
System.out.println(" 异常文件:" + errorFileCount);
System.out.println(" 成功文件:" + successFileCount);
System.out.println(" 成功文件夹:" + successFilesCount);
System.out.println( errorMsg.length() == 0 ? "未发生异常" : " 打印异常信息: " + errorMsg.toString() );
System.out.println(" <-----------------------------------------------------------> ");
}
}
- END -