Comparing and updating .properties files in java
A tutorial on how to compare and update two .properties files
Although it may be a rare case in this tutorial we will see a simple program to compare two .properties files,update the propeties with new values and merge the new .properties that might exist
package com.javaonly.comparableTest;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Properties;
import java.util.Set;
public class PropertiesDifference {
public static void main(String args[]) {
Properties propertiesOld = new Properties();
Properties propertiesNew = new Properties();
File fileOld = new File(
"C:\\java2\\workspace\\PropertiesComparator\\src\\com\\javaonly\\comparableTest\\p1.properties");
File fileNew = new File(
"C:\\java2\\workspace\\PropertiesComparator\\src\\com\\javaonly\\comparableTest\\p2.properties");
File fileTest = new File(
"C:\\java2\\workspace\\PropertiesComparator\\src\\com\\javaonly\\comparableTest\\test.properties");
try {
propertiesOld.load(new FileInputStream(fileOld));
propertiesNew.load(new FileInputStream(fileNew));
BufferedReader reader = new BufferedReader(new InputStreamReader(
new DataInputStream(new FileInputStream(fileOld)),"ISO-8859-1"));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(fileTest),"ISO-8859-1"));
String valueNew = "";
String valueOld = "";
String propertyName = "";
while ((propertyName = reader.readLine()) != null) {
//read the property key from p1 file
String[] split=propertyName.split("=");
propertyName=split[0].trim();
//get the property value from p1 and p2 files
valueOld = propertiesOld.getProperty(propertyName);
valueNew = propertiesNew.getProperty(propertyName);
//if there no value in p1 file just write the property key
if(valueOld==null){
writer.append(propertyName);
writer.append("\n");
}else{
//if there is no value for the property key in p2 file use the value in the p1 file
if (valueNew == null) {
writer.append(propertyName+"="+valueOld);
writer.append("\n");
} else {
//else for the property key write the value that is in p2 file
writer.append(propertyName+"="+valueNew);
writer.append("\n");
}
}
}
// finally write the properties that are only in p2 file
BufferedReader reader1=new BufferedReader(new InputStreamReader(
new DataInputStream(new FileInputStream(fileNew))));
writer.append("\n\n\n\n");
writer.append("#New added properties");
writer.append("\n\n\n\n");
while ((propertyName = reader1.readLine()) != null) {
String[] split=propertyName.split("=");
propertyName=split[0].trim();
valueOld = propertiesOld.getProperty(propertyName);
valueNew = propertiesNew.getProperty(propertyName);
if (valueOld == null) {
writer.append(propertyName+"="+valueNew);
writer.append("\n");
}
}
writer.flush();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Copyright © 2012 Design and Development Nikos Lianeris

- 1

- 1




