今天搞个重建索引工作,一开始想要把建索引的文件目录删除然后再来重建,但是在单个java文件main中运行可以做到,但是到了系统中,执行删除文件目录和下面的子文件时候,有个_0.cfs文件删除老是删除不掉,然后就想到把已经建立的索引全部删除来做到,示例如下:
private void deleteAllIndex() {
IndexReader reader = null;
File file = new File(indexDirStr);
if(file.exists() && file.isDirectory()) {
try{
reader = IndexReader.open(indexDirStr);
for(int i = 0; i < reader.maxDoc(); i ++) {
reader.deleteDocument(i);
}
reader.close();
} catch (Exception ex) {
} finally {
if(reader != null) {
try {
reader.close();
} catch (IOException e) {
}
}
}
}
}