对比两个properties文件差异

这个Java程序实现了对两个properties文件的对比,将只存在于其中一个文件的键值对以及不同键相同值的差异输出到一个新的文件中。程序首先读取两个文件,然后通过遍历找出差异,并将结果按格式写入到目标文件。该工具对于配置文件的管理和比对非常实用。
摘要由CSDN通过智能技术生成

控制台打印并输出文件

package com.ruoyi.web.controller.test;

import java.io.*;
import java.util.ArrayList;

/**
 * @ClassName: DiffProperties
 * @Description: properties文件对比工具类
 * @Author:
 * @Date:
 */
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());
		}
	}

	/**
	 * 比较两个properties文件
	 * @param file1
	 * @param file2
	 * @param file3
	 */
	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();

	}

	/**
	 * 获取文件中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;
		}
	}
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

@淡 定

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值