使用lucene建立索引时,出现write.lock,求高手修改,代码如下

packageorg.whu.edu.irb.lucene;importjava.io.BufferedReader;importjava.io.File;importj... package org.whu.edu.irb.lucene;

import java.io.BufferedReader;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;

import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.Directory;

import org.apache.lucene.store.SimpleFSDirectory;
import org.apache.lucene.util.Version;

public class TestFileIndex {
public static void main(String[] args) throws IOException {
File fileDir = new File("E:\\Test");// 表示需索引文件存放的地址;
File indexDir = new File("E:\\luceneindex");// 表示文件索引后将要存放的地址
Analyzer luceneanlyzer = new StandardAnalyzer(Version.LUCENE_30);
Directory dir = new SimpleFSDirectory(indexDir);
IndexWriter indexWriter = new IndexWriter(dir, luceneanlyzer, true,
IndexWriter.MaxFieldLength.UNLIMITED);
File[] files = fileDir.listFiles();
System.out.println(files.length);
for (int i = 0; i < files.length; i++) {
Document doc = new Document();
doc.add(new Field("content",
fileReader(files[i].getAbsolutePath()), Field.Store.NO,
Field.Index.ANALYZED));
doc.add(new Field("filename", files[i].getName(), Field.Store.YES,
Field.Index.ANALYZED_NO_NORMS));
indexWriter.addDocument(doc);

}
System.out.println("numDocs:" + indexWriter.numDocs());
indexWriter.close();
}

public static String fileReader(String filepath) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader(new File(
filepath)));
String line = new String();
String temp = new String();
while ((line = reader.readLine()) != null) {
temp += line;

}
reader.close();
return temp;
}

}
展开
 我来答
lj20000423
2014-06-28 · TA获得超过8.1万个赞
知道大有可为答主
回答量:2.2万
采纳率:24%
帮助的人:6003万
展开全部
我感觉应该是fileReader这个函数出错了,如果fileDir目录下有子目录,或者这个目录下有比较大的文件,获取内容就会失败,这样就不会执行ndexWriter.close();这句。程序中应该加一些异常判断和保护就可以了。
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式