3

I have a locally stored ini file which I am trying to parse as follows:

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

But I keep getting a parse error exception message pointing to the line after the comment line of the ini file (#). This is what my ini file looks like:

File.ini

#Tue Oct 11 18:45:03 CST 2016
PVIDNO=PALMUS-00001
PVIDNo=SSI-1
Authentication=VALID
ModelNo=KD03816-B001
PALMUS-ID=73364
PV-ID=PALMUS-01

4

3 回答 3

2

您可以使用以下方法执行相同操作Properties

 Properties p = new Properties();
 p.load(new FileInputStream("user.props"));
 System.out.println("user = " + p.getProperty("DBuser"));
 System.out.println("password = " + p.getProperty("DBpassword"));
 System.out.println("location = " + p.getProperty("DBlocation"));

.ini 文件在哪里:

# this a comment
! this a comment too
DBuser=anonymous
DBpassword=&8djsx
DBlocation=bigone
于 2016-10-11T11:12:12.183 回答
2

您正在使用一些来自谁知道哪里的类Ini ;并且该 Ini-File 解析器根本不喜欢包含“#comment”条目的 .ini 文件。

因此,您的选择基本上是:

  1. 我首先忘记了这一点,但也许是“最佳”选项:不要使用“ini”文件;但更改为“属性”文件;对于 Java 应用程序来说,这是一个更“自然”的选择。对它们的“内置”支持;嘿,“# comments”开箱即用。
  2. 如果Ini是“你自己的代码”;然后你让你自己的代码接受这样的评论
  3. 如果Ini来自某个库,那么您检查该库是否允许影响解析过程以允许此类注释。

如果库不允许这种特殊处理,您还有另外两个选择:

  1. 寻找其他一些 3rd 方库来解析您的文件
  2. 与提供您当前使用的图书馆的人“交谈”,并说服他们以某种方式让他们的图书馆为您服务。
于 2016-10-11T11:08:55.590 回答
1

您是否尝试过使用属性?

创建配置:

属性 prop = new Properties(); 输出流输出=空;

try {
        SaveSucessful = true;
    output = new FileOutputStream("config.jar");

    // set the properties value
    prop.setProperty("PVIDNO", "PALMUS-00001");

    // save properties to project root folder
    prop.store(output, null);

} catch (IOException io) {
    io.printStackTrace();
} finally {
    if (output != null) {
        try {
            output.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    }

读取配置:

Properties prop = new Properties();
    InputStream input = null;

    try {
            LoadSucessful = true;
        input = new FileInputStream("config.jar");

        // load a properties file
        prop.load(input);

        // get the property value and print it out
        PlayerName = prop.getProperty("PVIDNO");

    } catch (IOException ex) {
        ex.printStackTrace();
    } finally {
        if (input != null) {
            try {
                input.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

这应该可以完美地工作。

于 2016-10-11T11:11:19.203 回答