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

查看:1078
本文介绍了在Java中解析INI文件的最简单方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为Java中的遗留应用程序编写替代品。其中一个要求是旧应用程序使用的ini文件必须按原样读入新的Java应用程序。这个ini文件的格式是常见的windows样式,带有header部分和key = value对,使用#作为注释字符。

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.

我尝试使用Properties类来自Java,但当然如果不同标题之间存在名称冲突,那将无效。

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

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

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

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

这是一个关于库如何的示例used:

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));

这篇关于在Java中解析INI文件的最简单方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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