我修改了lucene的demo包的IndexHTML類
IndexHTML類
import org
import org
import org
import org
import org
import org
import java
import java
//還需調用demo的其他類
import org
/**
* Create html file index for searching
* @author tyrone
*
*/public class IndexHTML { private String DocsPath=null;
/**
* the path for index file;
*/ private String IndexFilePath=null;
/**
* true during deletion pass
*/ private boolean deleting = false;
/**
* existing index
*/ private IndexReader reader;
/**
* new index being built
*/ private IndexWriter writer;
/**
* document id iterator
*/ private TermEnum uidIter;
private void indexDocs(File file)throws Exception {
if (file
{
// if a directory String[] files = file
// list its files Arrays
// sort the files for (int i =
i++) // recursively index them this
} else if (file
// construct uid for doc
while (uidIter
uidIter
if (deleting) {
// delete stale docs
System
HTMLDocument
reader
}
uidIter
}
if (uidIter
uidIter
uidIter
// keep matching docs
} else if (!deleting) {
// add new docs
Document doc = HTMLDocument
System
writer
}
} else { // creating a new index
Document doc = HTMLDocument
System
writer
// add docs unconditionally
}
} return;
}
/**
* Walk directory hierarchy in uid order
* existing index in sync
* (a) old documents to be deleted;
* (b) unchanged documents
* or (c) new documents
*/ private void indexDocs(File file
throws Exception {
if (!create) {
// incrementally update
reader = IndexReader
// open existing index
uidIter = reader
// init uid iterator
this
if (deleting) {
// delete rest of stale docs
while (uidIter
System
HTMLDocument
reader
uidIter
}
deleting = false;
}
uidIter
// close uid iterator
reader
// close existing index
} else
// don
this
}
/**
* if create=true
* @param create
*/ public void run(boolean create)
{
try {
String index =
File root = null;
if (this
{
// index file path
index = this
}
if (this
System
return;
}
root = new File(this
Date start = new Date();
/**
* not create then maintenance
*/
if (!create) {
// delete stale docs
this
this
}
writer = new IndexWriter(index
writer
this
// add new docs
System
writer
writer
Date end = new Date();
System
System
} catch (Exception e) {
System
}
return;
}
/**
* @return Returns the IndexFilePath
*/ public String getIndexFilePath() { return IndexFilePath;
}
/**
* @param IndexFilePath The IndexFilePath to set
*/ public void setIndexFilePath(String property
}
/**
* @return Returns the DocsPath
*/ public String getDocsPath() { return DocsPath;
}
/**
* @param DocsPath The DocsPath to set
*/ public void setDocsPath(String property
}
/**
* test
* @param args
*/ public static void main(String[] args){ IndexHTML ih=new IndexHTML();
ih
ih
運行後生成
搜索文件類
/*
* Created on
*
* TODO To change the template for this generated file go to
* Window
*/package limas
/** * @author tyrone * * TODO To change the template for this generated type comment go to
* Window
*/public class HitsHTMLDoc { private String Title;
priva
From:http://tw.wingwit.com/Article/program/Java/ky/201311/28449.html