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

后端 未结 2 919
再見小時候
再見小時候 2021-01-29 09:48

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

|
|--src/main/resouces
       |
       |-updatePropertiesFile.java
       |-xyz_en_US.properties
       |-xyz_e         


        
相关标签:
2条回答
  • 2021-01-29 10:39

    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();
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-29 10:49

    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...
        }
    }
    
    0 讨论(0)
提交回复
热议问题