属性文件,列表作为单个键的值 [英] Properties file with a list as the value for an individual key

查看:139
本文介绍了属性文件,列表作为单个键的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的程序,我想从属性文件中读取一个键,并为键值创建一个相关的值列表。
最近我在尝试像这样

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 seperated list then split the value after the properties file is loaded. For example

a=one,two,three
b=nine,ten,fourteen

这篇关于属性文件,列表作为单个键的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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