差异文件夹
/* Copyright (C) 2007-2011 BlueXML - www.bluexml.com This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. *//*from NowJava.com - 时代Java*/ //package com.nowjava; import java.io.File; import java.io.IOException; import java.io.Writer; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class Main { public static final String COMPARE_SAME = "="; public static final String COMPARE_ADDED = "+"; public static final String COMPARE_DELETED = "-"; public static Map<String, List<String>> diffFolder(File folder1, File folder2, String filter) {/*时 代 J a v a - nowjava.com 提 供*/ Map<String, List<String>> diff = new HashMap<String, List<String>>(); List<String> allFilesPath1 = makeRelativeTo( listAllAsFilePath(folder1), folder1.getAbsolutePath()); List<String> allFilesPath2 = makeRelativeTo( listAllAsFilePath(folder2), folder2.getAbsolutePath()); if (filter.indexOf(COMPARE_SAME) != -1) { List<String> commonFiles = new ArrayList<String>(allFilesPath1); commonFiles.retainAll(allFilesPath2); diff.put(COMPARE_SAME, commonFiles); } if (filter.indexOf(COMPARE_DELETED) != -1) { List<String> onlyIn1 = new ArrayList<String>(allFilesPath1); onlyIn1.removeAll(allFilesPath2); diff.put(COMPARE_DELETED, onlyIn1); } if (filter.indexOf(COMPARE_ADDED) != -1) { List<String> onlyIn2 = new ArrayList<String>(allFilesPath2); onlyIn2.removeAll(allFilesPath1); diff.put(COMPARE_ADDED, onlyIn2); } return diff; } public static void diffFolder(File folder1, File folder2, Writer log, String filter) { Map<String, List<String>> diff = diffFolder(folder1, folder2, filter); try { log.write("DIFF " + folder1.getAbsolutePath() + " --> " + folder2.getAbsolutePath() + "\n"); for (Map.Entry<String, List<String>> ent : diff.entrySet()) { for (String v : ent.getValue()) { log.write(ent.getKey() + " file://" + v + "\n"); } } log.close(); } catch (IOException e) { e.printStackTrace(); } } private static List<String> makeRelativeTo(List<String> l, String root) { List<String> newList = new ArrayList<String>(); for (String s : l) { if (s.startsWith(root)) { newList.add(s.substring(root.length())); } } return newList; } public static List<String> listAllAsFilePath(File folder) { List<