如何使用 Java 读取包含部分的配置文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3728823/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to read a config file with sections using Java
提问by tshepang
Given a file containing this:
给定一个包含以下内容的文件:
[upper]
a = A
b = B
[words]
1 = one
2 = two
How do I access these key/values with reference to their headers? Java's Properties() class only handles section-less files.
如何参考它们的标头访问这些键/值?Java 的 Properties() 类只处理无节文件。
回答by Chris Dennett
Use the ini4j library (tutorial linked): http://ini4j.sourceforge.net/tutorial/
使用 ini4j 库(教程链接):http://ini4j.sourceforge.net/tutorial/
回答by ssahmed555
If you are a fan of the Apache Commons offerings, they have a library just for you: Commons Configuration. Commons Configuration reads many more formats other than just the .INI style files.
如果您是 Apache Commons 产品的粉丝,他们有一个专为您准备的库:Commons Configuration。Commons Configuration 不仅可以读取 .INI 样式文件,还可以读取更多格式。