-->

How to get all properties files in a package and u

2020-05-09 17:19发布

问题:

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

回答1:

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...
    }
}


回答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();
        }
    }
}