控制台打印并输出文件
package com.ruoyi.web.controller.test;
import java.io.*;
import java.util.ArrayList;
public class DiffProperties {
public static void main(String[] args) {
try {
String file1 = "D:\\develop\\application1.properties";
String file2 = "D:\\develop\\application2.properties";
File file3 = new File("D:\\develop\\different.txt");
if(! file3.exists()){
file3.createNewFile();
}
DiffProperties.compareProperties(file1, file2,file3);
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
}
public static void compareProperties(String file1, String file2, File file3) throws IOException {
ArrayList<Property> list1 = DiffProperties.getPropList(file1);
ArrayList<Property> list2 = DiffProperties.getPropList(file2);
ArrayList<String> only1 = new ArrayList<String>();
ArrayList<String> only2 = new ArrayList<String>();
ArrayList<String> diff = new ArrayList<String>();
for(Property p1:list1){
boolean found = false;
String tmpValue = "";
for(Property p2:list2){
if(p2.getKey().equals(p1.getKey())){
found = true;
tmpValue = p2.getValue();
}
}
if(!found){
only1.add(p1.getKey()+" = "+p1.getValue()+"\n");
} else if(!tmpValue.equals(p1.getValue())){
diff.add("Key: "+p1.getKey()+"\n"+"value1 = "+p1.getValue()+"\n"+"value2 = "+tmpValue+"\n");
}
}
for(Property p2:list2){
boolean found = false;
for(Property p1:list1){
if(p1.getKey().equals(p2.getKey())){
found = true;
}
}
if(!found){
only2.add(p2.getKey()+" = "+p2.getValue()+"\n");
}
}
FileWriter fw = new FileWriter(file3);
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("文件1: "+file1+'\n');
stringBuilder.append("文件2: "+file2+'\n');
System.out.println("文件1: "+file1);
System.out.println("文件2: "+file2);
stringBuilder.append("========================只存在文件1中的: "+'\n');
System.out.println("========================只存在文件1中的: ");
for(String str:only1){
stringBuilder.append(str);
System.out.println(str);
}
stringBuilder.append("========================只存在文件2中的: "+'\n');
System.out.println("========================只存在文件2中的: ");
for(String str:only2){
stringBuilder.append(str+'\n');
System.out.println(str);
}
stringBuilder.append("========================两个文件都有,但value不同的 : "+'\n');
System.out.println("========================两个文件都有,但value不同的 : ");
for(String str:diff){
stringBuilder.append(str+'\n');
System.out.println(str);
}
fw.write(stringBuilder.toString());
fw.close();
}
public static ArrayList<Property> getPropList(String file){
ArrayList<Property> pList = new ArrayList<Property>();
try{
FileInputStream fileInputStream = new FileInputStream(file);
DataInputStream in = new DataInputStream(fileInputStream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while ((strLine = br.readLine()) != null) {
if(!strLine.contains("#") && strLine.contains("=")){
pList.add(new Property(strLine));
}
}
in.close();
} catch (Exception e){
e.printStackTrace();
}
return pList;
}
static class Property {
private String key;
private String value;
public Property(String str) {
super();
str = str.trim();
String[] tmpArr = str.split("=",2);
this.key = tmpArr[0].trim();
this.value = tmpArr[1].trim();
}
public String getKey() {
return key;
}
public String getValue() {
return value;
}
}
}