package com.tool; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.apache.commons.io.FileUtils; public class FileUtil { public static void main(String[] args) { start("E:\\web_space\\***", "E:\\g\\trunk\\***"); start("E:\\web_space\\***", "E:\\g\\trunk\\***"); } private static void start(String fromDir, String toDir) { List<String> fileNameList = getFile(fromDir); for (String fileName : fileNameList) { System.out.println("from : " + fileName); String toFileName = fileName.replace(fromDir, toDir); System.out.println("to : " + toFileName); File from = new File(fileName); File to = new File(toFileName); moveFile(from, to); } } private static boolean isSvn(String dir) { if (dir.endsWith(".svn")) { return true; } return false; } /** * 获得所有文件名的list(不包括svn). * * @param path * @return */ private static List<String> getFile(String path) { File file = new File(path); // 得到当前目录下的所有 文件/文件夹 File[] array = file.listFiles(); List<String> fileNameList = new ArrayList<String>(); for (int i = 0; i < array.length; i++) { if (array[i].isFile()) { fileNameList.add(array[i].getPath()); } else if (array[i].isDirectory()) { if (isSvn(array[i].getPath())) { continue; } // 如果是文件夹就继续添加 fileNameList.addAll(getFile(array[i].getPath())); } } return fileNameList; } /** * 移动文件from到to. * * @param from * 源文件 * @param to * 目标文件 * @throws IOException */ public static void moveFile(File from, File to) { try { FileUtils.copyFile(from, to); } catch (IOException e) { throw new RuntimeException("移动文件时出错", e); } } }