Development projects would need internationalization generally. Most people use property files backed by a ResourceBundle to support it. Developers tend to keep different property files for different countries and keep adding new keys and values whenever they need. If you are a developer, you might have already been there in a situation where once in a while you would need to clean up the property files and remove duplicates. Removing the duplicate keys and values from a property file helps you to reduce the size of the file. Here’s how to remove duplicates from a properties file easily.

If you do the removal of duplicates from property file manually, it will take years to finish. Instead, you can do it programmatically so that you can do it less than a second. Here’s such piece of code which is written to remove the duplicate keys and values from a property file. It takes the input from a file and writes output to a different file. It is written in Java, you can copy and paste the code to any editor and run directly without any additional dependencies.

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;

public class PropertiesTask {
  public static void main(String[] args) throws IOException {
    Properties prop = new Properties();

    FileInputStream fis = new FileInputStream("PATH_TO_INPUT_PROPERTY_FILE");
    prop.load(fis);
    System.out.println(prop.size());

    Enumeration e1 = prop.propertyNames();
    List<String> list = new ArrayList<String>();

    while (e1.hasMoreElements()) {
      String s1 = (String) e1.nextElement();
      if (list.contains(prop.getProperty(s1))) {
        prop.remove(s1);
      }
      list.add(prop.getProperty(s1));
    }

    System.out.println(prop.size());
    FileOutputStream fos = new FileOutputStream("PATH_TO_OUTPUT_PROPERTY_FILE");

    prop.store(fos, "Removed duplicates");

  }
}

 

You need to replace PATH_TO_INPUT_PROPERTY_FILE with the path to the property file to be processed. Also, PATH_TO_OUTPUT_PROPERTY_FILE should be replaced with the output file name you want.

The above code is just basic and you are welcome to make any modifications to it such as reading a bunch of files and process it together.

Share this article:

Leave a Reply

Your email address will not be published. Required fields are marked *