初學Lucene
這個想法的來源是
開發工具與運行環境
思路分析與設計
整個程序裡
主類
異常類
還有一個文件過濾工廠類(FileFilterFactory)
異常類不是必要的
IndexJavaFiles
/**
*indexthejavasourcefiles
*/
package powerwind;
import java
import java
import org
import org
/**
*@authorPowerwind
*@version
*/
publicclass IndexJavaFiles {
/**
*默認構造方法
*/
public IndexJavaFiles() {
}
/**
* 這個私有遞歸方法由index方法調用
*
*@paramwriter
*@paramfile
*@paramff
*@throwsIndexException
*/
privatevoid indexDirectory(IndexWriter writer
if (file
// 有選擇地(過濾)獲取目錄下的文件和目錄
File[] files = file
// 非空目錄
if (files != null) {
for (int i =
indexDirectory(writer
}
}
} else {
try {
// 這裡的file經過先前的過濾
writer
System
} catch (IOException ioe) {
thrownew IndexException(ioe
}
}
}
/**
*傳參數是文件就直接索引
*
*@paramwriter
*@paramfile
*@paramff
*@throwsIndexException
*/
publicvoid index(IndexWriter writer
// 確定可讀
if (file
if (file
indexDirectory(writer
} elseif (filter
try {
writer
System
} catch (IOException ioe) {
thrownew IndexException(ioe
}
} else {
System
}
}
}
/**
*@paramfile
*
*把File變成Document
*/
private Document parseFile(File file) throws IndexException {
Document doc = new Document();
doc
Field
try {
doc
} catch (FileNotFoundException fnfe) {
thrownew IndexException(fnfe
}
return doc;
}
}
index(IndexWriter writer
下面是IndexException異常類
IndexException
package powerwind;
publicclass IndexException extends Exception {
public IndexException(String message) {
super(
}
}
下面是FileFilterFactory類
FileFilterFactory
package powerwind;
import java
publicclass FileFilterFactory {
/**
*靜態匿名內部類
*/
privatestatic FileFilter filter = new FileFilter() {
publicboolean accept(File file) {
long len;
return file
(file
((len = file
}
};
publicstatic FileFilter getFilter() {
returnfilter;
}
}
main方法
/**
* main方法
*/
publicstaticvoid main(String[] args) throws Exception {
IndexJavaFiles ijf = new IndexJavaFiles();
Date start = new Date();
try {
IndexWriter writer = IndexWriterFactory
System
ijf
System
writer
writer
Date end = new Date();
System
} catch (IOException e) {
System
}
}
SearchJavaFiles
package powerwind;
import java
import org
import org
import org
import org
import org
import org
publicclass SearchJavaFiles {
private IndexSearcher searcher;
private QueryParser parser;
/**
*
*@paramsearcher
*/
public SearchJavaFiles(IndexSearcher searcher) {
this
}
/**
*
*@paramfield
*@paramanalyzer
*/
publicvoid setParser(String field
setParser(new QueryParser(field
}
/**
*@paramparser
*/
publicvoid setParser(QueryParser parser) {
this
}
/**
*
*@paramquery
*@returnHits
*@throwsSearchException
*/
public Hits serach(Query query) throws SearchException {
try {
returnsearcher
} catch (IOException ioe) {
thrownew SearchException(ioe
}
}
/**
*
*@paramqueryString
*@returnHits
*@throwsSearchException
*/
public Hits serach(String queryString) throws SearchException {
if (parser == null)
thrownew SearchException(
try {
returnsearcher
} catch (IOException ioe) {
thrownew SearchException(ioe
} catch (ParseException pe) {
thrownew SearchException(pe
}
}
/**
*
*輸出hits的結果
*
*@paramhits
*@paramstart
*@paramend
*@throwsSearchException
*/
publicstatic Hits display(Hits hits
try {
while (start < end) {
Document doc = hits
String path = doc
if (path != null) {
System
} else {
System
}
start++;
}
} catch (IOException ioe) {
thrownew SearchException(ioe
}
return hits;
}
main方法
/**
*@paramargs
*/
publicstaticvoid main(String[] args) throws Exception {
String field =
String index =
finalint rows_per_page =
finalchar NO =
SearchJavaFiles sjf = new SearchJavaFiles(new IndexSearcher(IndexReader
sjf
BufferedReader in = new BufferedReader(new InputStreamReader(System
while (true) {
System
String line = in
if (line == null || line
System
break;
}
Hits hits = sjf
System
int len = hits
int i =
if (len >
while (true) {
if (i + rows_per_page >= len) {
SearchJavaFiles
break;
} else {
SearchJavaFiles
System
line = in
if (line
break;
}
}
else
System
}
}
}
SearchException
package powerwind;
publicclass SearchException extends Exception {
public SearchException(String message) {
super(
}
}
完善設想
能夠處理Zip文件Jar文件
通過反射機制索引class類文件
除控制台輸入輸出外
圖形界面操作
索引文件時
From:http://tw.wingwit.com/Article/program/Java/hx/201311/25898.html