We had a properties file that had massive numbers of duplicate property definitions. Since the Java convention is that the last definition of a property is the one that will be used, I created a simple utility that outputs a properties file with non-final definitions skipped:
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.HashMap;
/*
* File UniqueProperties.java
* Created on Aug 19, 2005 by jstell
*
*/
/**
* Simple utility to remove the non-final
* occurrences of a property in a
* properties file. The unique content is
* written to Standard Out.
*/
public class UniqueProperties
{
public static void main(String[] args)
throws Exception
{
if (args.length != 1)
System.err.
println("USAGE:n java " +
UniqueProperties.class.getName() +
" [props file]");
String in = args[0];
BufferedReader frIn =
new BufferedReader(new FileReader(in));
int lineNum = 0;
ArrayList linesToSkip = new ArrayList();
HashMap propLines = new HashMap();
String line = null;
// figure out what lines to skip
while ((line = frIn.readLine()) != null)
{
lineNum ++;
if (line.indexOf('=') > 0 &&
!line.startsWith("#"))
{
String pName =
line.substring(0,
line.indexOf('=')).trim();
Object prevLine =
propLines.put(pName,
new Integer(lineNum));
if (prevLine != null)
linesToSkip.add(prevLine);
}
}
// start over, output skipped lines
frIn.close();
frIn = new BufferedReader(new FileReader(in));
lineNum = 0;
while ((line = frIn.readLine()) != null)
{
lineNum ++;
if (!linesToSkip.
contains(new Integer(lineNum)))
System.out.println(line);
}
}
} // UniqueProperties