0

我需要为文本文件(至少 20 kb)编写一个解析器,并且我需要确定一组单词中的单词是否出现在这个文本文件中(大约 400 个单词和数字)。所以我正在寻找最有效的方法来做到这一点(如果找到匹配项,我需要对此进行进一步处理,这是前一行)。

我目前所做的是排除不包含任何信息的行(元数据行的种类),然后逐字比较 - 但我不认为只逐字比较是最有效的可能性。

谁能提供一些提示/提示/想法/...

非常感谢你

4

2 回答 2

1

这取决于您对“高效”的含义。

如果您想要一种非常直接的编码方式,请记住 java 中的 String 对象具有方法 String.contains(CharSequence sequence)。

然后,您可以将文件内容放入一个字符串中,然后使用方法 contains() 迭代您想要检查的关键字是否出现在字符串中。

于 2012-08-01T10:05:22.630 回答
0

以下情况如何:

Put all your keywords in a HashSet (Set<String> keywords;)
Read the file one line at once
  For each line in file:
  Tokenize to words
  For each word in line:
  If word is contained in keywords (keywords.containes(word))
    Process actual line
    If previous line is available
        Process previous line
  Keep track of previous line (prevLine = line;)
于 2012-08-01T10:51:52.963 回答