ITeye博客

自动对比2个配置文件的差异

lengyimeng · 2014-12-23 13:51 · java · 编程语言 · Markdown

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<String, Properties> map_143 = new HashMap<String, Properties>();

public static HashMap<String, Properties> map_144 = new HashMap<String, Properties>();

 

/**

* @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<String, Properties> 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 > 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<String, Properties> map143,

HashMap<String, Properties> 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<Object> keys = p144.keySet();

List<Object> lackKeys = new ArrayList<Object>();

for (Object o : keys) {

if(p143.containsKey(o))continue;

lackKeys.add(o);

}

if(lackKeys.size()>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<Object> 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<Object> 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包在附件

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。