【发布时间】:2015-12-09 18:49:07
【问题描述】:
我有一个文件,其文本如下:
4 abiogenezele
5 abiogenezelor
6 acefaliile
7 acefaliilor
8 acetonuriile
....
它的格式为 (ID_WORD WORD) 并有一个近似值。约 33000 字。
我想输入一个词来找到他的ID。
我试试这个代码。它有效,但效率不高。
int ID;
String word = "acefaliile";
String pattern = "(?i)([\\d]+) ("+word+")";
Pattern r = Pattern.compile(pattern);
boolean found = false;
// Read the file
try (BufferedReader br = new BufferedReader(new FileReader("./resources/txt/lemma.txt"))) {
String line;
while ((line = br.readLine()) != null) {
Matcher m = r.matcher(line);
if (m.find( )) {
// m.group(1) is ID
// m.group(2) is WORD
ID = Integer.parseInt(m.group(1));
found=true;
break;
}
}
if(!found) {
ID = 0;
}
}
【问题讨论】:
-
使用数据库,
sqlite之类的可能比较理想。或者只是将它们加载到内存中。 -
将文件加载到内存并执行二分查找。
标签: java algorithm file search