How to parse ini file with sections in Java?

吃可爱长大的小学妹 提交于 2020-08-06 15:03:58

问题


Here is a sample ini file:

[link#1]
alias=My Link 1
link=https://www.yandex.ru/

[link#2]
alias=My Link 2
link=https://mail.ru/

[link#3]
alias=My Link 3
link=http://point.md/ru/

I've seen how to parse it, but the keys are the same, and I need to get this into lets say ArrayList<LinkObject> . Does anyone know a good solution for this? Or shall we format the .ini file differently?


回答1:


I can suggest you ini4j. This is small and very easy to use library. Just download files or include dependency into your maven config:

<dependency>
   <groupId>org.ini4j</groupId>
   <artifactId>ini4j</artifactId>
   <version>0.5.4</version>
</dependency>

Now to load .ini file just do:

Wini ini = new Wini(new File("your file path"));

Examples of usage:

//output names of all sections    
Collection<Profile.Section> list = ini.values();
for(Profile.Section section : list){
    System.out.println(section.getName());
}

//fetch and output option value
String value = ini.fetch("link#1", "alias");
System.out.println(value);

//output keys of all options of one section
Profile.Section section = ini.get("link#1");
for(String key : section.keySet()){
   System.out.println(key);
}


来源:https://stackoverflow.com/questions/34637174/how-to-parse-ini-file-with-sections-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!