在Java中解析INI文件最简单的方法是什么?(What is the easiest way to parse an INI file in Java?)

编程入门 行业动态 更新时间:2024-10-26 14:28:51
在Java中解析INI文件最简单的方法是什么?(What is the easiest way to parse an INI file in Java?)

我正在为Java中的遗留应用程序写入替换。 其中一个要求是使用旧应用程序的ini文件必须按原样读取到新的Java应用程序中。 这个ini文件的格式是通用的窗口样式,标题部分和键=值对,使用#作为注释的字符。

我尝试使用Java中的Properties类,但是当不同标题之间有名称冲突时,这当然不起作用。

所以问题是,读这个INI文件和访问密钥最简单的方法是什么?

I am writing a drop-in replacement for a legacy application in Java. One of the requirements is that the ini files that the older application used have to be read as-is into the new Java Application. The format of this ini files is the common windows style, with header sections and key=value pairs, using # as the character for commenting.

I tried using the Properties class from Java, but of course that won't work if there is name clashes between different headers.

So the question is, what would be the easiest way to read in this INI file and access the keys?

最满意答案

我使用的图书馆是ini4j 。 它是轻量级的,并轻松解析ini文件。 此外,它不会对10,000个其他jar文件使用任何深奥的依赖,因为其中一个设计目标是仅使用标准的Java API

这是一个关于如何使用库的例子:

Ini ini = new Ini(new File(filename)); java.util.prefs.Preferences prefs = new IniPreferences(ini); System.out.println("grumpy/homePage: " + prefs.node("grumpy").get("homePage", null));

The library I've used is ini4j. It is lightweight and parses the ini files with ease. Also it uses no esoteric dependencies to 10,000 other jar files, as one of the design goals was to use only the standard Java API

This is an example on how the library is used:

Ini ini = new Ini(new File(filename)); java.util.prefs.Preferences prefs = new IniPreferences(ini); System.out.println("grumpy/homePage: " + prefs.node("grumpy").get("homePage", null));

更多推荐

本文发布于:2023-07-28 08:33:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1303047.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:最简单   文件   方法   INI   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!