在Java中解析INI文件最简单的方法是什么?

问题描述 投票:101回答:11

我正在用Java替代传统应用程序。要求之一是必须将旧应用程序使用的ini文件原样读取到新的Java应用程序中。此ini文件的格式是常见的Windows样式,带有标头部分和键=值对,使用#作为注释字符。

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

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

java parsing ini
11个回答
118
投票

我使用的库是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));

0
投票

hoat4的解决方案非常优雅且简单。它适用于所有sane ini文件。但是,我已经看到许多在key中具有转义空格字符的字符。为了解决这个问题,我下载并修改了java.util.Properties的副本。虽然这有点不合常规,而且是短期的,但实际的mod只是几行而已,非常简单。我将向JDK社区提出一个建议,以包括这些更改。通过添加内部类变量:

private boolean _spaceCharOn = false;

我控制与扫描键/值分离点有关的处理。我用一个小的私有方法替换了空格字符搜索代码,该方法根据上述变量的状态返回一个布尔值。

private boolean isSpaceSeparator(char c) {
    if (_spaceCharOn) {
        return (c == ' ' || c == '\t' || c == '\f');
    } else {
        return (c == '\t' || c == '\f');
    }
}

此方法在私有方法load0(...)中的两个地方使用。还有一种公共方法可以打开它,但是如果您的应用程序不希望使用空格分隔符,则最好使用原始版本的Properties


-1
投票

就这么简单.....

//import java.io.FileInputStream;
//import java.io.FileInputStream;

Properties prop = new Properties();
//c:\\myapp\\config.ini is the location of the ini file
//ini file should look like host=localhost
prop.load(new FileInputStream("c:\\myapp\\config.ini"));
String host = prop.getProperty("host");

62
投票

作为mentioned,可以使用ini4j来实现。我再举一个例子。

如果我们有这样的INI文件:

[header]
key = value

以下应显示value到STDOUT:

Ini ini = new Ini(new File("/path/to/file"));
System.out.println(ini.get("header", "key"));

检查the tutorials以获取更多示例。


28
投票

简单到80行:

package windows.prefs;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class IniFile {

   private Pattern  _section  = Pattern.compile( "\\s*\\[([^]]*)\\]\\s*" );
   private Pattern  _keyValue = Pattern.compile( "\\s*([^=]*)=(.*)" );
   private Map< String,
      Map< String,
         String >>  _entries  = new HashMap<>();

   public IniFile( String path ) throws IOException {
      load( path );
   }

   public void load( String path ) throws IOException {
      try( BufferedReader br = new BufferedReader( new FileReader( path ))) {
         String line;
         String section = null;
         while(( line = br.readLine()) != null ) {
            Matcher m = _section.matcher( line );
            if( m.matches()) {
               section = m.group( 1 ).trim();
            }
            else if( section != null ) {
               m = _keyValue.matcher( line );
               if( m.matches()) {
                  String key   = m.group( 1 ).trim();
                  String value = m.group( 2 ).trim();
                  Map< String, String > kv = _entries.get( section );
                  if( kv == null ) {
                     _entries.put( section, kv = new HashMap<>());   
                  }
                  kv.put( key, value );
               }
            }
         }
      }
   }

   public String getString( String section, String key, String defaultvalue ) {
      Map< String, String > kv = _entries.get( section );
      if( kv == null ) {
         return defaultvalue;
      }
      return kv.get( key );
   }

   public int getInt( String section, String key, int defaultvalue ) {
      Map< String, String > kv = _entries.get( section );
      if( kv == null ) {
         return defaultvalue;
      }
      return Integer.parseInt( kv.get( key ));
   }

   public float getFloat( String section, String key, float defaultvalue ) {
      Map< String, String > kv = _entries.get( section );
      if( kv == null ) {
         return defaultvalue;
      }
      return Float.parseFloat( kv.get( key ));
   }

   public double getDouble( String section, String key, double defaultvalue ) {
      Map< String, String > kv = _entries.get( section );
      if( kv == null ) {
         return defaultvalue;
      }
      return Double.parseDouble( kv.get( key ));
   }
}

16
投票

这是一个简单但功能强大的示例,使用apache类HierarchicalINIConfiguration

HierarchicalINIConfiguration iniConfObj = new HierarchicalINIConfiguration(iniFile); 

// Get Section names in ini file     
Set setOfSections = iniConfObj.getSections();
Iterator sectionNames = setOfSections.iterator();

while(sectionNames.hasNext()){

 String sectionName = sectionNames.next().toString();

 SubnodeConfiguration sObj = iniObj.getSection(sectionName);
 Iterator it1 =   sObj.getKeys();

    while (it1.hasNext()) {
    // Get element
    Object key = it1.next();
    System.out.print("Key " + key.toString() +  " Value " +  
                     sObj.getString(key.toString()) + "\n");
}

通用配置的编号为runtime dependencies。至少需要commons-langcommons-logging。根据您所使用的内容,可能需要其他库(有关详细信息,请参见前面的链接)。


13
投票

或者使用标准Java API,您可以使用java.util.Properties

Properties props = new Properties();
try (FileInputStream in = new FileInputStream(path)) {
    props.load(in);
}

10
投票

在18行中,将java.util.Properties扩展为可分为多个部分:

public static Map<String, Properties> parseINI(Reader reader) throws IOException {
    Map<String, Properties> result = new HashMap();
    new Properties() {

        private Properties section;

        @Override
        public Object put(Object key, Object value) {
            String header = (((String) key) + " " + value).trim();
            if (header.startsWith("[") && header.endsWith("]"))
                return result.put(header.substring(1, header.length() - 1), 
                        section = new Properties());
            else
                return section.put(key, value);
        }

    }.load(reader);
    return result;
}

2
投票

另一个选项是Apache Commons Config,还有一个用于从INI files加载的类。它确实有一些runtime dependencies,但是对于INI文件,它只需要Commons集合,lang和日志记录。

我已经在项目中使用Commons Config及其属性和XML配置。它非常易于使用,并支持一些非常强大的功能。


2
投票

您可以尝试JINIFile。是Delphi的TIniFile的翻译,但适用于Java

https://github.com/SubZane/JIniFile


2
投票

我个人更喜欢Confucious

很好,因为它不需要任何外部依赖关系,它很小-仅16K,并在初始化时自动加载您的ini文件。例如:

Configurable config = Configuration.getInstance();  
String host = config.getStringValue("host");   
int port = config.getIntValue("port"); 
new Connection(host, port);
© www.soinside.com 2019 - 2024. All rights reserved.