gpt4 book ai didi

java - 按字母顺序对 .properties 文件中的键进行排序,同时忽略大小写

转载 作者:行者123 更新时间:2023-12-02 04:35:12 33 4
gpt4 key购买 nike

我有一个函数,它将 .properties 文件中的值存储到树形图 (translatedMap) 中,然后从“keyMap”中检索新值并将它们存储到“translatedMap”中。问题是,无论我做什么,似乎总是将大写键与非大写键分开。这是我的代码:

Properties translation = new Properties(){

private static final long serialVersionUID = 1L;

@Override
public synchronized Enumeration<Object> keys() {

return Collections.enumeration(new TreeSet<Object>(super
.keySet()));
}

};


//creates file and stores values of keyMap into the file
try {
TreeMap<String, String> translatedMap = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);

InputStreamReader in = new InputStreamReader(new FileInputStream(filePath), "UTF-8");
translation.load(in);

// Store all values to TreeMap and sort
Enumeration<?> e = translation.propertyNames();
while (e.hasMoreElements()) {
String key = (String) e.nextElement();
if (key.matches(".#")) {
} else {
String value = translation.getProperty(key);
translatedMap.put(key, value);
}
}

// Add new values to translatedMap
for (String key : keyMap.keySet()) {
// Handle if some keys have already been added; delete so they can be re-added
if (translatedMap.containsKey(key)) {
translatedMap.remove(key);
}
translatedMap.put(key, keyMap.get(key));
}

in.close();
translation.putAll(translatedMap);
File translationFile = new File(filePath);

OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(translationFile, false), "UTF-8");
translation.store(out, null);

out.close();
} catch (IOException e) {
e.printStackTrace();
}
}

我得到的输出类似于:

CAPITALIZED_KEY1=value1
CAPITALIZED_KEY2=value2
alowercase.key=value3
anotherlowercase.key=value4
morelowercase.keys=value5

当我希望它出现时:

alowercase.key=value3
anotherlowercase.key=value4
CAPITALIZED_KEY1=value1
CAPITALIZED_KEY2=value2
morelowercase.keys=value5

最佳答案

属性未排序。无论您插入其中的顺序是什么,或者如果您使用已排序的内容调用 putAll(),它们都会扩展 Hashtable。请参阅here .

关于java - 按字母顺序对 .properties 文件中的键进行排序,同时忽略大小写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30916495/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com