如何获取包中的所有属性文件并使用java更新键值? [英] How to get all properties files in a package and update the key values using java?

查看:86
本文介绍了如何获取包中的所有属性文件并使用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

这是我的项目结构。

我有一个updatePropertiesFile类,用于更新所有属性的键文件。我有大约200个属性文件。

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.

我尝试使用resoucebundle机制。但是使用资源包,我们只能得到一个属性文件。我尝试了 ResourceBundle.getBundle(String,Locale) ResourceBundle.getBundle(String)方法。

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.

我这样做:

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.

请帮我维护评论和订单

推荐答案

每个Java IDE都有一个替换路径功能。 Sublime Text,VS Code和Atom几乎肯定也有。 IntelliJ特别好。没有理由甚至编写Java代码来执行此操作。否则,它就像这样简单:

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

这篇关于如何获取包中的所有属性文件并使用java更新键值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆