1、把某一磁盘目录下的所有文件(包含磁盘的深层目录) 复制到指定的一个目录的下
例如 e:/ 下所有递归到的pdf文件,复制到 d:/temp目录下
package daoImpl;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Scanner;
/**
* @ClassName: CopyFolder.java
* @Description: TODO(把某一磁盘目录递归到的指定文件类型,复制到某一目录下)
*
* @author Administrator
* @E-mail 809044093@qq.com
* @version V1.0
* @Date 2014-3-19 上午09:11:46
*/
public class CopyFolder {
//允许复制的文件类型
public static String[] filterFile = { ".java", ".xml", ".xdl",
".properties", ".sql", ".jupiter", ".wsdl" , ".pdf", ".html" };
private long total = 0l;
public static void main(String[] args) throws Exception {
Scanner scanner = new Scanner(System.in);
String answer = null;
//源路径
File src = new File("e:/");
//目标路径
File des = new File("D:/temp");
new CopyFolder().copyFolder(src, des, filterFile);
}
/**
*
* @Title: copyFolder
* @Description: TODO(复制文件)
* @param: @param srcFolder(源路径)
* @param: @param destFolder(目标路径)
* @param: @param filterFile(允许复制的文件类型)
* @param: @throws Exception
* @return: void
* @throws
*/
public void copyFolder(File srcFolder, File destFolder, String[] filterFile)
throws Exception {
File[] files = srcFolder.listFiles();
for (File file : files) {
if (file.isFile()) {
String pathname = destFolder + File.separator + file.getName();
for (String suff : filterFile) {
if (pathname.endsWith(suff)) {
File dest = new File(pathname);
File destPar = dest.getParentFile();
destPar.mkdirs();
if (!dest.exists()) {
dest.createNewFile();
}
copyFile(file, dest);
}
}
} else {
copyFolder(file, destFolder, filterFile);
}
}
}
/**
*
* @Title: copyFile
* @Description: TODO(这里用一句话描述这个方法的作用)
* @param: @param src
* @param: @param dest
* @param: @throws Exception
* @return: void
* @throws
*/
private void copyFile(File src, File dest) throws Exception {
FileInputStream input = null;
FileOutputStream outstrem = null;
try {
input = new FileInputStream(src);
outstrem = new FileOutputStream(dest);
outstrem.getChannel().transferFrom(input.getChannel(), 0,input.available());
total++;
String temp =String.format("\ncopy:%s size:%s to: %s complate: %s", src,src.length(),dest,total);
System.out.print(temp);
} catch (Exception e) {
throw e;
} finally {
outstrem.flush();
outstrem.close();
input.close();
}
}
}
2、递归复制文件到另外一个目录下
/**
* 复制一个目录及其子目录、文件到另外一个目录
* @param src
* @param dest
* @throws IOException
*/
private void copyFolder(File src, File dest) throws IOException {
if (src.isDirectory()) {
if (!dest.exists()) {
dest.mkdir();
}
String files[] = src.list();
for (String file : files) {
File srcFile = new File(src, file);
File destFile = new File(dest, file);
// 递归复制
copyFolder(srcFile, destFile);
}
} else {
InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dest);
byte[] buffer = new byte[1024];
int length;
while ((length = in.read(buffer)) > 0) {
out.write(buffer, 0, length);
}
in.close();
out.close();
}
}
方法二
public void test(){
File file = new File("E:/findbugs-2.0.2");
File tofile = new File("c:/test");
try {
FileUtils.copyDirectory(file, tofile);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}