Jince Martin Jince Martin - 2 months ago 12
Java Question

How to get all properties files in a package and update the key values using java?

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.

Answer

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