---
title: "自动对比2个配置文件的差异"
author: "lengyimeng"
date: "2014-12-23T13:51:12Z"
tags: []
categories: ["编程语言"]
slug: "2"
draft: false
lastmod: "2014-12-23T13:51:12Z"
id: 2169489
url: "https://lengyimeng.iteye.com/blog/2169489"
---

package compareConfFile;

 

import java.io.File;

import java.io.IOException;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Properties;

import java.util.Set;

 

import org.springframework.core.io.FileSystemResource;

import org.springframework.core.io.Resource;

import org.springframework.util.DefaultPropertiesPersister;

import org.springframework.util.PropertiesPersister;

 

public class CompareFile {

public static HashMap&lt;String, Properties&gt; map\_143 = new HashMap&lt;String, Properties&gt;();

public static HashMap&lt;String, Properties&gt; map\_144 = new HashMap&lt;String, Properties&gt;();

 

/\**

\* @param args

\* @throws IOException

\*/

public static void main(String\[] args) throws IOException {

initData("C:\\\\143conf", map\_143);

initData("C:\\\\144conf", map\_144);

System.out.println("143上配置文件数:"+map\_143.size()+"----144上配置文件数:"+map\_144.size());

compareMap(map\_143, map\_144);

//test("rmapi.properties");

}

 

public static void initData(String filepath, HashMap&lt;String, Properties&gt; map)

throws IOException {

File file = new File(filepath);

File\[] files = file.listFiles();

if(files==null || files.length==0){

System.out.println("文件夹路径错误---filepath="+filepath);

}

if (files != null && files.length &gt; 0) {

for (File f : files) {

if (f.isDirectory())

continue;

if (!f.getName().endsWith(".conf")

&& !f.getName().endsWith(".properties"))

continue;

Resource resource = new FileSystemResource(f);

PropertiesPersister propertiesPersister = new DefaultPropertiesPersister();

Properties props = new Properties();

propertiesPersister.load(props, resource.getInputStream());

map.put(f.getName().toLowerCase(), props);

}

}

}

 

public static void compareMap(HashMap&lt;String, Properties&gt; map143,

HashMap&lt;String, Properties&gt; map144) {

if(map143==null || map143.isEmpty() || map144==null || map144.isEmpty()){

System.out.println("文件夹里面的配置文件没有读取到，请先确定文件夹路径是否正确");

return;

}

StringBuilder sb = new StringBuilder();

for(String s:map144.keySet()){

if(map143.containsKey(s)){

compareProperties(s, map143.get(s), map144.get(s));

}else{

sb.append(s+",");

}

}

System.out.println("143相对于144上缺少的配置文件为:"+sb.toString());

}

 

public static void compareProperties(String fileName,Properties p143,Properties p144) {

System.out.println("name="+fileName+"----143\_size="+p143.size()+"----144\_size="+p144.size());

if(p143==null || p143.isEmpty() || p144==null || p144.isEmpty()){

System.out.println("配置文件有为空的情况----------fileName="+fileName);

System.out.println("\\r\\n");

return;

}

Set&lt;Object&gt; keys = p144.keySet();

List&lt;Object&gt; lackKeys = new ArrayList&lt;Object&gt;();

for (Object o : keys) {

if(p143.containsKey(o))continue;

lackKeys.add(o);

}

if(lackKeys.size()&gt;0){

System.out.println("在文件"+fileName+"中143上需要补充的配置项为:");

for(Object o:lackKeys){

System.out.println(o);

}

}

System.out.println("\\r\\n");

}

 

public static void test(String key){

Properties p143 = map\_143.get(key);

Set&lt;Object&gt; keys143 = p143.keySet();

System.out.println("143----keys=:");

StringBuilder sb143 = new StringBuilder();

for(Object o:keys143){

sb143.append(o+",");

}

System.out.println(sb143.toString());

Properties p144 = map\_144.get(key);

Set&lt;Object&gt; keys144 = p144.keySet();

System.out.println("144----keys=:");

StringBuilder sb144 = new StringBuilder();

for(Object o:keys144){

sb144.append(o+",");

}

System.out.println(sb144.toString());

}

 

}

最近在做一期系统升级到二期的工作，相关数据库、配置文件的差异我都写了点代码自动进行对比，这样相对于人工去做的话效率可能更高一点，另外出错的概率也小点。

这里面最主要的代码就是红色部分，就是把非.properties后缀的文件转化为properties对象

所需jar包在附件