文件1:app1.properties
sms.registerTemplate = SMS_123456
sms.registerExpireTime = 600
billcode.size = 200
feign.name.produce = abc-produce
yrx.cloud.applicationId = 777888999444555666
文件2:app2.properties
sms.registerTemplate = SMS_123456333
sms.registerExpireTime = 600
billcode.size = 210
feign.name.produce = abc-produce
yrx.cloud.applicationId = 777888999444555666
#新增项
yrx.cloud.yuxin = yuruixin
比较结果:
文件1: D:\code\yrx\yrx-supplier\src\test\java\com\yrx\supplier\diff\app1.properties
文件2: D:\code\yrx\yrx-supplier\src\test\java\com\yrx\supplier\diff\app2.properties
========================只存在文件1中的:
========================只存在文件2中的:
yrx.cloud.yuxin = yuruixin
========================两个文件都有,但value不同的 :
Key: sms.registerTemplate
value1 = SMS_123456
value2 = SMS_123456333
Key: billcode.size
value1 = 200
value2 = 210
源码:
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
/**
* @ClassName: DiffProperties
* @Description: properties文件对比工具类
* @Author: RuiXin Yu
* @Date: 2019/2/11 10:00
*/
public class DiffProperties {
public static void main(String[] args) {
try {
String file1 = "D:\\code\\yrx\\yrx-supplier\\src\\test\\java\\com\\yrx\\supplier\\diff\\app1.properties";
String file2 = "D:\\code\\yrx\\yrx-supplier\\src\\test\\java\\com\\yrx\\supplier\\diff\\app2.properties";
DiffProperties.compareProperties(file1, file2);
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
}
/**
* 比较两个properties文件
* @param file1
* @param file2
*/
public static void compareProperties(String file1,String file2){
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");
}
}
System.out.println("文件1: "+file1);
System.out.println("文件2: "+file2);
System.out.println("========================只存在文件1中的: ");
for(String str:only1){
System.out.println(str);
}
System.out.println("========================只存在文件2中的: ");
for(String str:only2){
System.out.println(str);
}
System.out.println("========================两个文件都有,但value不同的 : ");
for(String str:diff){
System.out.println(str);
}
}
/**
* 获取文件中k&v
* @param file
* @return
*/
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;
}
}
}
如果你有更好的解决方案,欢迎留言交流!