import java.io.*; import java.util.ArrayList; import java.util.List; public class Test { public static void main(String[] args) { String oldFileName = "D:\\test\\old.properties"; String newFileName = "D:\\test\\new.properties"; List<String> lines1 = getLines(oldFileName); List<String> lines2 = getLines(newFileName); List<Pro> pList1 = getPro(lines1); List<Pro> pList2 = getPro(lines2); List<Pro> p2NotContain = getTargetNotContain(pList1, pList2); List<Pro> p1NotContain = getTargetNotContain(pList2, pList1); System.out.println("需要增加的配置:"); System.out.println(toProListString(p1NotContain)); System.out.println("需要删除的配置:"); System.out.println(toProListString(p2NotContain)); } private static List<Pro> getTargetNotContain(List<Pro> source, List<Pro> target) { List<Pro> ret = new ArrayList<Pro>(); for(Pro p1 : source){ boolean isFoundInP2 = false; for(Pro p2 : target){ if(p1.getKey().equals(p2.getKey())){ isFoundInP2 = true; break; } } if(!isFoundInP2){ ret.add(p1); } } return ret; } private static String toProListString(List<Pro> list){ String ret = ""; for(Pro item : list){ ret += toProString(item); ret += "\n"; } return ret; } private static String toProString(Pro pro){ String ret = ""; if(pro.getNote() != null && !"".equals(pro.getNote())){ ret+= pro.getNote() + "\n"; } ret += (pro.getKey() + "=" + pro.getValue()); return ret; } private static List<Pro> getPro(List<String> lines) { List<Pro> ret = new ArrayList<Pro>(); String note = null; String key = null; String value = null; for(String line : lines){ if(line == null || "".equals(line)){ continue; } if(line.startsWith("#")){ note = line; }else{ int index = line.indexOf("="); key = line.substring(0, index); value = line.substring(index + 1, line.length()); ret.add(new Pro(note, key, value)); note = null; key = null; value = null; } } return ret; } public static List<String> getLines(String fileName){ List<String> lines=new ArrayList<String>(); try { BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(fileName),"UTF-8")); String line= null; while ((line= br.readLine()) != null) { lines.add(line); } br.close(); } catch (FileNotFoundException e){ }catch (IOException e){} return lines; } private static class Pro{ private String note; private String key; private String value; public Pro(String note, String key, String value) { this.note = note; this.key = key; this.value = value; } public String getNote() { return note; } public void setNote(String note) { this.note = note; } public String getKey() { return key; } public void setKey(String key) { this.key = key; } public String getValue() { return value; } public void setValue(String value) { this.value = value; } } }