更改ini文件中的键值分隔符

时间:2013-06-10 08:03:31

标签: java ini4j

我有一个以||作为键值分隔符的ini文件。我想使用ini4j库解析它,但每次运行代码时它都会给我一个错误(异常)。我认为ini4j仅将=:标识为键值分隔符。有没有其他方法可以在使用ini4j时解析文件?

这是我的示例文件

[header]
one||1
two||2

[section1] 
three||3
four||4
five||5
six||6

[section2]
seven||7
eight||8
nine||9

这是我的示例代码

 public static void main(String[] args) throws IOException {
    Ini ini=new Ini(new File("/home/esunmes/NetBeansProjects/ini4jexample/src/ini4jexample/newfile2.ini"));
    for(String str:ini.keySet())
    {System.out.println(str);
     Ini.Section section=ini.get(str);
     for(String key:section.keySet())
     {System.out.println("the key is : "+key);// TODO code application logic here
}

}

我也想知道这种情况:key || value1 || value2

1 个答案:

答案 0 :(得分:1)

如何用预期的=替换分隔符?

    String inputPropties = "KEY1||VALUE1\nKEY2||VALUE2\nKEY3||VALUE3";
    inputPropties = inputPropties.replaceAll("\\|\\|","=");
    Properties properties = new Properties();
    properties.load(new StringReader(inputPropties));
    for( String key: properties.stringPropertyNames()) {
        System.out.println(key+" = "+properties.getProperty(key));
    }
相关问题