【发布时间】:2016-01-20 01:36:30
【问题描述】:
我做了一个简单的搜索功能,在大量 html 文件中查找字符串 (+8000) 这是我的代码:
public Boolean loadAssetTextAsString(Context context, String search,String name) {
Boolean exist=false;
BufferedReader in = null;
try {
StringBuilder buf = new StringBuilder();
InputStream is = context.getAssets().open(name);
in = new BufferedReader(new InputStreamReader(is));
String str;
boolean isFirst = true;
while ( (str = in.readLine()) != null ) {
if (isFirst)
isFirst = false;
else
if (str.toLowerCase(Locale.getDefault()).contains(search)){
Log.e(" SUCC",str);
exist=true;
break;
} else { Log.e(" Fail",":(");
}
}
return exist;
} catch (IOException e) {
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
}
}
}
return exist ;
}
问题:
问题是这种方法非常慢而且根本没用。
有没有办法或算法在短时间内搜索大量文件?
【问题讨论】:
-
你想要一个倒排索引而不是线性搜索(就像一本书后面的那个)。您可能不想自己实现它,这就是 Lucene(Java 搜索库)的用途。
标签: java android html database search