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

人盡茶涼 提交于 2019-12-02 14:12:32

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

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();
        }
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!