Properties file with a list as the value for an individual key(带有列表作为单个键值的属性文件)
问题描述
对于我的程序,我想从属性文件中读取一个键以及该键的相关值列表.
最近我也在这样尝试
For my program I want to read a key from a properties file and an associated List of values for the key.
Recently I was trying like that
public static Map<String,List<String>>categoryMap = new Hashtable<String, List<String>>();
Properties prop = new Properties();
try {
prop2.load(new FileInputStream(/displayCategerization.properties));
Set<Object> keys = prop.keySet();
List<String> categoryList = new ArrayList<String>();
for (Object key : keys) {
categoryList.add((String)prop2.get(key));
LogDisplayService.categoryMap.put((String)key,categoryList);
}
System.out.println(categoryList);
System.out.println("Category Map :"+LogDisplayService.categoryMap);
keys = null;
prop = null;
} catch (Throwable e) {
e.printStackTrace();
}
我的属性文件如下 -
and my properties file is like below -
A=APPLE
A=ALPHABET
A=ANT
B=BAT
B=BALL
B=BUS
我希望密钥 A 应该有一个列表,其中包含 [APPLE, ALPHABET,ANT]
和 B 包含 [BAT,BALL,BUS]
.
所以地图应该是这样的 {A=[APPLE, ALPHABET,ANT], B=[BAT,BALL,BUS]}
但我得到
{A=[ANT], B=[BUS]}
我在互联网上搜索了这种方法,但一无所获.我希望应该有办法.有什么帮助吗?
I want for key A there should be a list which contain [APPLE, ALPHABET,ANT]
and B contain [BAT,BALL,BUS]
.
So Map should be like this {A=[APPLE, ALPHABET,ANT], B=[BAT,BALL,BUS]}
but I get
{A=[ANT], B=[BUS]}
I searched on the internet for such a way but found nothing. I wish there should be a way.
Any help?
推荐答案
尝试将属性写成逗号分隔的列表,然后在加载属性文件后拆分值.例如
Try writing the properties as a comma separated list, then split the value after the properties file is loaded. For example
a=one,two,three
b=nine,ten,fourteen
您也可以使用 org.apache.commons.configuration 并使用 AbstractConfiguration.setListDelimiter(char) 方法如果您在值中使用逗号.
You can also use org.apache.commons.configuration and change the value delimiter using the AbstractConfiguration.setListDelimiter(char) method if you're using comma in your values.
这篇关于带有列表作为单个键值的属性文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:带有列表作为单个键值的属性文件
基础教程推荐
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 降序排序:Java Map 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01