【问题标题】:ini4j API - read all keys of section and create a map of itini4j API - 读取部分的所有键并创建它的映射
【发布时间】:2016-01-04 18:24:30
【问题描述】:

我是 ini4j API http://mvnrepository.com/artifact/org.ini4j/ini4j 的新手。我有一个要求,如果将key 作为CT 传递,那么它应该检查所有section 并在Java Map 中填充键值对(如10 = CT,11 = CT 等)。我们如何做到这一点?

城市不是固定的,可能是很多城市。我在这里只展示了 4 个。

我的小部件.ini

[CapeTown] 
10=CT
11=CT
12=CT
13=JO
14=JO
15=CT
17=WN

[Wonderers] 
21=CT
22=JO
23=WN
24=JO
25=CT
26=CT
27=CT

[Johanbege]
30=CT
31=CT
32=JO
33=JO
34=CT
35=CT
36=WN

[Durban]
40=CT
41=CT
42=JO
43=JO
44=CT
45=CT
46=WN

我开始的代码:

public class CityReader {
    public static void main(String[] args) throws InvalidFileFormatException, IOException {
        File file = new File("src/main/resources/widget.ini");
        Ini ini = new Ini(file);

    }
}

【问题讨论】:

    标签: java ini4j


    【解决方案1】:

    我为你开发了一些东西。希望这能解决您的问题。

    import java.io.File;
    import java.io.IOException;
    import java.util.HashMap;
    import java.util.Map;
    
    import org.ini4j.Ini;
    import org.ini4j.InvalidFileFormatException;
    import org.ini4j.Profile.Section;
    
    public class CityReader {
        public static void main(String[] args) throws InvalidFileFormatException, IOException {
            Map<String, String> mapCT = new HashMap<String, String>();
            Map<String, String> mapJO = new HashMap<String, String>();
    
            File file = new File("src/main/resources/widget.ini");
            Ini ini = new Ini(file);
            for (String sectionName: ini.keySet()) {
                Section section = ini.get(sectionName);
                for (String optionKey: section.keySet()) {
                    if(section.get(optionKey).equals("CT"))
                        mapCT.put(optionKey, section.get(optionKey));
                    if(section.get(optionKey).equals("JO"))
                        mapJO.put(optionKey, section.get(optionKey));
                }
            }
            System.out.println(mapCT);
            System.out.println(mapJO);
        }
    }
    

    如果您想做一些动态的事情,请使用以下内容:

    public class CityReader {
        public static void main(String[] args) throws InvalidFileFormatException, IOException {
            File file = new File("src/main/resources/widget.ini");
            Ini ini = new Ini(file);
    
            Map<String, String> mapkey = new HashMap<String, String>();
    
            List<String> key = new ArrayList<String>();
            key.add("CT");
            key.add("JO");
    
            for (String s : key) {
                for (String sectionName: ini.keySet()) {
                    Section section = ini.get(sectionName);
                    for (String optionKey: section.keySet()) {
                        if(section.get(optionKey).equals(s))
                            mapkey.put(optionKey, section.get(optionKey));
                    }
                }
            }
            System.out.println(mapkey);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-24
      • 1970-01-01
      • 1970-01-01
      • 2019-07-23
      • 2017-05-19
      相关资源
      最近更新 更多