如何确保Lucene在索引后离开文件?
我正在使用Lucene为XML文件编制索引。文件将进入输入目录,编制索引并移动到输出目录 在某些情况下,它工作正常,但很少有文件失败 当我尝试使用Windows命令提示符重新创建该文件时,它显示该文件已在使用,这告诉我java进程仍然连接到该文件 有人能帮我确保Lucene java进程在索引后离开文件吗 这是我正在尝试的代码如何确保Lucene在索引后离开文件?,lucene,Lucene,我正在使用Lucene为XML文件编制索引。文件将进入输入目录,编制索引并移动到输出目录 在某些情况下,它工作正常,但很少有文件失败 当我尝试使用Windows命令提示符重新创建该文件时,它显示该文件已在使用,这告诉我java进程仍然连接到该文件 有人能帮我确保Lucene java进程在索引后离开文件吗 这是我正在尝试的代码 int originalNumDocs = writer.numDocs(); for (File f : queue) {
int originalNumDocs = writer.numDocs();
for (File f : queue) {
FileReader fr = null;
try {
Document doc = new Document();
//===================================================
// add contents of file
//===================================================
fr = new FileReader(f);
doc.add(new TextField("contents", fr));
String targetFileStr = IOUtils.toString(new FileInputStream(f), "UTF-8");
doc.add(new StringField("xmlContent", targetFileStr, Field.Store.YES));
doc.add(new StringField("path", f.getPath(), Field.Store.YES));
doc.add(new StringField("filename", f.getName(), Field.Store.YES));
writer.addDocument(doc);
System.out.println("Added: " + f);
} catch (Exception e) {
System.out.println("Could not add: " + f);
e.printStackTrace();
} finally {
fr.close();
File afile = f;
if(afile.renameTo(new File("C:/Personal/Logging/OutputDir/" + afile.getName()))){
System.out.println("File is moved successful!");
}else{
System.out.println("File is failed to move!");
}
}
}
int newNumDocs = writer.numDocs();
System.out.println("");
System.out.println("************************");
System.out.println((newNumDocs - originalNumDocs) + " documents added.");
System.out.println("************************");
writer.commit();
queue.clear();
我每30秒打一次电话。它在用雄猫跑 行中的
文件输入流是否可以:
String targetFileStr = IOUtils.toString(new FileInputStream(f), "UTF-8");
开着门吗?或许可以尝试将其更改为:
FileInputStream fileStream = new FileInputStream(f);
String targetFileStr = IOUtils.toString(fileStream, "UTF-8");
并在finally
块中调用fileStream.close()