什么是用Java解析文件的最佳方法

chi*_*esh 2 java

我有一个带有Tag - Value格式数据的文本文件.我想解析这个文件以形成一个Trie.什么是最好的方法?

文件样本:("#"中的字符串是一个标签,'#'用于注释该行.)

 #Hi, this is a sample file.

"abcd" = 12;
"abcde" = 16;
"http" = 32;
"sip" = 21;
Run Code Online (Sandbox Code Playgroud)

Esk*_*sko 5

请阅读使用属性和修剪多余的部分(",;和空格).简短的例子:

Properties props = Properties.load(this.getClass()
                                       .getResourceAsStream("path/to.file"));
Map<String, String> cleanedProps = new HashMap<String, String>();
for(Entry pair : props.entrySet()) {
    cleanedProps.put(cleanKey(pair.getKey()),
                     cleanValue(pair.getValue()));
}
Run Code Online (Sandbox Code Playgroud)

请注意,在上面的解决方案中,您只需要实现cleanKey()cleanValue()自己.如果需要,您可能需要相应地更改数据类型,我仅使用字符串作为示例.


小智 5

这基本上是一个属性文件,我会删除"围绕标签,然后使用Properties类http://java.sun.com/javase/6/docs/api/java/util/Properties.html#load(java) .io.Reader)加载文件.