java.io.IOException: Cannot delete C:\Program Files\Apache Software Foundation\index_dir\write.lock
at org.apache.lucene.store.NativeFSLockFactory.clearLock(NativeFSLockFactory.java:143)
at org.apache.lucene.store.Directory.clearLock(Directory.java:141)
at org.apache.lucene.index.IndexWriter.init(IndexWriter.java:1541)
at org.apache.lucene.index.IndexWriter.<init>(IndexWriter.java:1074)
at com.tongdainfo.dao.forumupload.lucene.ForumuploadLuceneDaoImpl.rebuildAllIndex(ForumuploadLuceneDaoImpl.java:305)
at com.tongdainfo.server.forumupload.lucene.ForumuploadLuceneServiceImpl.rebuildForumuploadIndex(ForumuploadLuceneServiceImpl.java:33)
at com.lucene.service.test.Test.bulidIndex(Test.java:52)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.util.MethodInvoker.invoke(MethodInvoker.java:276)
at org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean$MethodInvokingJob.executeInternal(MethodInvokingJobDetailFactoryBean.java:260)
at org.springframework.scheduling.quartz.QuartzJobBean.execute(QuartzJobBean.java:86)
at org.quartz.core.JobRunShell.run(JobRunShell.java:203)
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:520)
code run here================================================我要使用quartz 进行建立索引,在第一次测试可以,但以后就会报以上错误。/**
 * 对所有上传文件进行重新索引
 */
public  void rebuildAllIndex(){
File file = new File(INDEX_DIR);
  System.out.println("-------建立索引--------------------------"+INDEX_DIR); 
if(file.exists()){
file.setWritable(true);
for(File subFile : file.listFiles()){

subFile.delete();
}
}else{
file.mkdirs();
}

List<Forumupload> data = this.forumuploadDao.findAll();

IndexWriter indexWriter = null;
try {
indexWriter = new IndexWriter(this.openDirectory(),
  getAnalyzer(),
  true,
  IndexWriter.MaxFieldLength.UNLIMITED);
//设置打开使用复合文件
//indexWriter.setUseCompoundFile(true);
int size = data == null ? 0 : data.size();
for (int i = 0; i < size; i++) {
Forumupload forumupload = data.get(i);
Document doc = createForumuploadDocument(forumupload);
indexWriter.addDocument(doc);

if(i % 20 == 0){
indexWriter.commit();
}
}
//indexWriter.optimize();  //对索引进行优化   
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (LockObtainFailedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(indexWriter != null){
indexWriter.close();//关闭IndexWriter,把内存中的数据写到文件
}
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();

}
}

对以前的索引文件进行删除!这是方法!

解决方案 »

  1.   


    if(file.exists()){
    file.setWritable(true);
    if(file.isFile())
    file.delete();
    else
    {
    for(File subFile : file.listFiles()){
    subFile.delete();}
    }}else{
    file.mkdirs();
    }还有你最好还是找找有什么用到的进程没有CLOSE 
      

  2.   

    file.setWritable(true); 改不了,也不知道怎么修改write.lock的路径