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

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

问题描述

我正在编写 Java 中遗留应用程序的替代品.要求之一是旧应用程序使用的 ini 文件必须按原样读入新的 Java 应用程序.这个ini文件的格式是常见的windows风格,有标题部分和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.

我尝试使用 Java 中的 Properties 类,但是如果不同标头之间存在名称冲突,那当然行不通.

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

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

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天全站免登陆