【问题标题】:Parsing error after '#' line in ini file在 ini 文件中的“#”行之后解析错误
【发布时间】:2017-02-19 21:52:41
【问题描述】:

我有一个本地存储的ini file,我试图解析如下:

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

但我不断收到 解析错误异常消息,该消息指向 ini 文件 (#) 的注释行之后的行。这是我的 ini 文件的样子:

文件.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

【问题讨论】:

  • Ini 类从何而来?这不是 Java 标准库中的类。通常你会使用 Properties 类来加载这种格式的文件。
  • @Jesper 忘了提到我为此使用了 ini4j。
  • 看看Click Me希望这能有所帮助。

标签: java parsing ini ini4j


【解决方案1】:

您正在使用来自 who-knows-where 的 someIni;并且该 Ini-File 解析器根本不喜欢包含“#comment”条目的 .ini 文件。

所以,您的选择基本上是:

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

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

  1. 寻找其他一些第三方库来解析您的文件
  2. 与提供您当前使用的图书馆的人“交谈”,并说服他们以某种方式让他们的图书馆为您服务。

【讨论】:

  • 我检查了库,它确实允许 #cmets。将尝试您所说的将其更改为属性的内容。谢谢!
【解决方案2】:

您是否尝试过使用属性?

创建配置:

属性 prop = new Properties(); OutputStream 输出 = null;

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

这应该可以完美运行。

【讨论】:

  • 您使用的是.jar 扩展名?认真的吗?
  • 将尝试使用属性。不过对 .jar 不太确定。
  • 使用任何你喜欢的扩展名,.jar 只是我使用的一个例子。
  • -Kylie Irwin,如果您不知道,您可以单击复选标记标记适合您的答案。
【解决方案3】:

你可以对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

【讨论】:

  • 我在哪里调用或启动 ini 文件?对不起。新手来了。
  • p.load() 方法。 p.load(new FileInputStream("user.props"));
猜你喜欢
  • 2016-07-27
  • 1970-01-01
  • 2012-11-18
  • 1970-01-01
  • 2021-01-06
  • 2018-08-30
  • 2012-09-24
  • 1970-01-01
  • 2019-02-02
相关资源
最近更新 更多