对比两个properties文件差异

控制台打印并输出文件

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
    评论
好的,这是一个关于文件夹比较的问题,需要用到 Katalon 和 java-diff-utils 库。 首先,我们需要在 Katalon 中导入 java-diff-utils 库。可以通过以下步骤实现: 1. 在 Katalon 中打开你的项目 2. 在左侧的 Project Explorer 中,右键单击你的项目文件夹,选择 Properties 3. 在 Properties 窗口中,选择 Java Build Path 4. 在 Libraries 选项卡中,单击 Add External JARs... 5. 在弹出的文件选择窗口中,选择你下载的 java-diff-utils 库的 JAR 文件,单击 OK 6. 现在,java-diff-utils 库已经添加到你的项目中了,你可以在 Katalon 中使用该库的类和方法了。 接下来,我们需要编写 Groovy 脚本来实现文件夹比较。以下是一个简单的实现,它可以比较两个文件夹下的同名文件,并将比较结果和差异数据输出到 result 文件中: ``` import difflib.* import java.nio.file.* // 定义两个待比较的文件夹路径 def folder1 = Paths.get("path/to/folder1") def folder2 = Paths.get("path/to/folder2") // 定义输出结果的文件路径 def resultFile = Paths.get("path/to/result.txt") // 删除旧的结果文件,重新生成结果文件 if (Files.exists(resultFile)) { Files.delete(resultFile) } Files.createFile(resultFile) // 获取文件夹1中的所有文件 def files1 = Files.list(folder1).map{it.toFile()}.toList() // 获取文件夹2中的所有文件 def files2 = Files.list(folder2).map{it.toFile()}.toList() // 取文件夹1和文件夹2中的所有文件名的交集 def fileNames = files1.collect{it.getName()}.intersect(files2.collect{it.getName()}) // 比较交集中的文件,在结果文件中输出比较结果和差异数据 fileNames.each { fileName -> def file1 = files1.find{it.getName() == fileName} def file2 = files2.find{it.getName() == fileName} if (file1 == null) { Files.write(resultFile, "文件 $fileName 只在文件夹2中存在\n".getBytes(), StandardOpenOption.APPEND) } else if (file2 == null) { Files.write(resultFile, "文件 $fileName 只在文件夹1中存在\n".getBytes(), StandardOpenOption.APPEND) } else { def diff = DiffUtils.diff(FileUtils.readLines(file1), FileUtils.readLines(file2)) if (diff.getDeltas().size() > 0) { Files.write(resultFile, "文件 $fileName 不同:\n".getBytes(), StandardOpenOption.APPEND) diff.getDeltas().each { delta -> Files.write(resultFile, " 差异位置:${delta.getOriginal().getPosition()}\n".getBytes(), StandardOpenOption.APPEND) Files.write(resultFile, " 差异内容:${delta.getOriginal().getLines()}\n".getBytes(), StandardOpenOption.APPEND) } } } } ``` 上述代码中,我们首先定义了两个待比较的文件夹路径和结果文件路径。然后,我们使用 Files.list() 方法获取文件夹1中的所有文件文件夹2中的所有文件,将它们转化为 File 类型的列表。接着,我们使用 collect() 方法取文件夹1和文件夹2中的所有文件名的交集。接下来,我们使用 each 方法遍历交集中的所有文件名,对于每个文件名,我们在文件夹1和文件夹2中查找同名文件。如果文件只在文件夹1中存在或者只在文件夹2中存在,我们将信息输出到结果文件中;如果文件两个文件夹中都存在,我们使用 java-diff-utils 库的 DiffUtils.diff() 方法进行比较,如果文件不同,我们将差异数据输出到结果文件中。需要注意的是,我们使用了 find() 方法来查找文件夹1和文件夹2中的同名文件,这样可以避免嵌套循环。 最后,我们需要在每次执行脚本时删除旧的结果文件,并重新生成结果文件。可以使用以下代码实现: ``` if (Files.exists(resultFile)) { Files.delete(resultFile) } Files.createFile(resultFile) ``` 希望这个回答能够帮助你解决问题!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

@淡 定

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

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

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

打赏作者

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

抵扣说明:

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

余额充值