0

I tried a lot, but was unable to find a solution.

|
|--src/main/resouces
       |
       |-updatePropertiesFile.java
       |-xyz_en_US.properties
       |-xyz_en_AF.properties
       |-xyz_en_AE.properties

This is my project structure.

I have a updatePropertiesFile class, to update a key for all the properties file. I have around 200 properties file.

So what I need is that, I need to write a method to update a particular key in all these properties file. Manual change is not that practically. I need to write an application which does this funtionality.

I tried using resoucebundle mechanism. But using resource bundle, we can get only one property file. I tried ResourceBundle.getBundle(String,Locale) and ResourceBundle.getBundle(String) methods.

I need an iteration through these properties file and updation for the key.

My initial issue is fixed.

I did this as :

File[] files = new File("src/main/resources").listFiles();
    for (File file : files) {
    if (file.getName().endsWith("properties"))  {
     //my logic
    }

But when I did this, my comments in properties file got removed and the order got changed. I wnat to keep the order of the key and the comments in properties file too.

To keep the order I tried using:

public static class LinkedProperties extends Properties {
        private final HashSet<Object> keys = new LinkedHashSet<Object>();

        public LinkedProperties() {
        }

        public Iterable<Object> orderedKeys() {
            return Collections.list(keys());
        }

        public Enumeration<Object> keys() {
            return Collections.<Object>enumeration(keys);
        }

        public Object put(Object key, Object value) {
            keys.add(key);
            return super.put(key, value);
        }
    }

But this caused some changes in key-value pair in properties file. Some special character got added up.

Please help me out to maintain the comments and order

Jince Martin
  • 301
  • 1
  • 9
  • 18
  • 1
    Read them as files, not as resource. There is impossible to update resource in java. – talex Sep 23 '16 at 13:18
  • Why is this so difficult, since all your files are in the same folder? Read each file, keep the contents in memory, update the required key and write the file back – Ironluca Sep 23 '16 at 13:23

2 Answers2

4

Every Java IDE out there has a replace-in-path function. Sublime Text, VS Code and Atom almost certainly have that as well. IntelliJ's particularly good. No reason to even write Java code to do this. Otherwise, it's as simple as:

File[] files = new File("src/main/resources").listFiles();
for (File file in files) {
    if (file.getName().endsWith("properties")) {
        //Load and change...
    }
}
Mike Thomsen
  • 36,828
  • 10
  • 60
  • 83
  • This worked. Thank you. But I have one more issue. When I did like this, the comments in my properties file got deleted and order of the key got chnaged. But I dont want those changes. I wanted the comments and need to maintain the order too. – Jince Martin Sep 26 '16 at 08:50
2

This code iterates over all files in a given directory and opens each .properties file. It then applies the changed values and stores the file again.

public void changePropertyFilesInFolder(File folder) {
    for(File file : folder.listFiles()) {
        if(file.isRegularFile() && file.getName().endsWith(".properties")) {
            Properties prop = new Properties();
            FileInputStream instream = new FileInputStream(file);
            prop.load(instream);
            instream.close();
            prop.setProperty("foo", "bar"); // change whatever you want here
            FileOutputStream outstream = new FileOutputStream(file);
            prop.store(outstream, "comments go here");
            outstream.close();
        }
    }
}
Hexaholic
  • 3,299
  • 7
  • 30
  • 39
  • This worked. Thank you. But I have one more issue. When I did like this, the comments in my properties file got deleted and order of the key got chnaged. But I dont want those changes. I wanted the comments and need to maintain the order too – Jince Martin Sep 26 '16 at 08:52
  • 1
    @JinceMartin Unfortunately, Java's `Properties` class is not built to handle any formatting of the `.properties` file, which includes both comments and order. See [this question](https://stackoverflow.com/questions/565932/a-better-class-to-update-property-files) for a possible alternative. – Hexaholic Sep 26 '16 at 08:55