//文件拷贝工具类
package com.corejava;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileUtil {
/**
*文件夹拷贝
* @throws CopyWrongException
*/
public void copyFolder(String fromPath, String toPath) throws CopyWrongException { // 具体实现如下
if (fromPath != null) {
File sf = new File(fromPath);
if (sf.exists()) {
//获取目录中子文件
File[] files = sf.listFiles();
String from = null;
String to = null;
//把目录中的子文件复制到目标文件
for (File f : files) {
if (f.isDirectory()) {
from = fromPath + "/" + f.getName();
to = toPath + "/" + f.getName();
//创建一个新的文件夹
creatDirectory(to);
//递归
copyFolder(from, to);
} else if (f.isFile()) {
//创建一个新文件
creatFile(toPath + "/" + f.getName());
//调用copyFile()方法把指定目录文件拷贝到目标文件
copyFile(fromPath + "/" + f.getName(), toPath + "/" + f.getName());
}
}
} else {
System.err.println("文件不存在!");
throw new CopyWrongException();
}