2016-10-11 29 views
3

我有一个本地存储ini file而我试图解析如下:分析错误的ini文件“#”线

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

但我不断收到指着线解析错误异常消息在ini文件(#)的注释行之后。这就是我的ini文件看起来像:

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

+1

哪里级'Ini'从何而来?这不是Java标准库中的类。通常你会使用类“Properties”来加载这种格式的文件。 – Jesper

+0

@Jesper忘了提及我使用ini4j。 –

+0

看看这个[点击我](http://stackoverflow.com/a/190633/1577167)希望这可以帮助。 – BottleMan

回答

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 
+0

虽然我在哪里打电话或启动ini文件?抱歉。新手在这里。 –

+0

p.load()方法。 p.load(new FileInputStream(“user.props”)); – granmirupa

+0

这对我来说非常合适。谢谢! –

2

您正在使用一些燕丽是来自谁也不知道的地方; Ini-File解析器根本不喜欢包含“#comment”条目的.ini文件。

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

  1. 我忘了这第一,但也许“最好”的选择:不使用“INI”文件;但改为“财产”文件;这对Java应用程序来说是一个更“自然”的选择。为他们提供“内置”支持;嘿,“#评论”开箱即用。
  2. If Ini是“你自己的代码”;那么你让你自己的代码接受这样的评论
  3. 如果Ini来自某个库,那么你检查该库是否允许影响解析过程以允许这样的评论。

如果库不允许这样的特殊处理,你有另外两个选择:

  1. 寻找出一些其他的第三方库文件
  2. “对话”解析为提供你正在使用的图书馆的人,并说服他们以某种方式让他们的图书馆为你工作。
+0

我检查过图书馆,它确实允许#评论。尽管你会尝试你所说的改变财产。谢谢! –

+0

欢迎您! – GhostCat

1

您是否尝试过使用属性?

创建配置:

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

这应该很好地工作。

+0

您正在使用'.jar'扩展名?真的吗? – fabian

+0

将尝试使用属性。虽然不太清楚.jar。 –

+0

使用任何你喜欢的扩展,.jar只是我用过的一个例子。 –