會員註冊 / 登入  |  電腦版  |  Jump to bottom of page

新生訓練營 Test » 最近看書用eclipse測試lucene的程式碼都有bug?不知那裡出錯了..(程式碼都書本一樣)

發表人: l98179012, 十級學員
2011-01-21 14:53:22

package ch2.lucenedemo.process;

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

import javax.swing.filechooser.FileFilter;

import jeasy.analysis.MMAnalyzer;

import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;

public class IndexProcesser {
// 成員變數儲存建立的索引檔案存放的位置
private String INDEX_STORE_PATH = "d:\\index";

// 建立索引
public void createIndex(String inputDir) {
try {
// MMAnalyzer作為分詞工具建立一個IndexWriter
IndexWriter writer = new IndexWriter(INDEX_STORE_PATH,
new MMAnalyzer(), true);
File filesDir = new File(inputDir);
// 取得所有需要建立索引的檔案陣列
File[] files = filesDir.listFiles();
// 檢查陣列
for (int i = 0; i < files.length; i++) {
// 獲得檔案名
String fileName = files[i].getName();
// 判斷檔案是否為txt類型的檔案
if (fileName.substring(fileName.lastIndexOf("."))
.equals(".txt")) {
// 建立一個新的Document
Document doc = new Document();
// 為檔案名建立一個Field
Field field = new Field("filename", files[i].getName(),
Field.Store.YES, Field.Index.TOKENIZED);
doc.add(field);
// 為檔案內容建立一個Filed
field = new Field("content", loadFileToString(files[i]),
Field.Store.NO, Field.Index.TOKENIZED);
doc.add(field);
// 把Document加入IndexWriter
writer.addDocument(doc);
}
}
// 關閉IndexWriter
writer.close();

} catch (Exception e) {
e.printStackTrace();
}

}

public static void main(String[] args) {
IndexProcesser processor = new IndexProcesser();
processor.createIndex("d:\\testfolder");
}
public String loadFileToString(File file) {
try {
BufferedReader br = new BufferedReader(new FileReader(file));
StringBuffer sb = new StringBuffer();
String line = br.readLine();
while (line != null) {
sb.append(line);
line = br.readLine();
}
br.close();
return sb.toString();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}

}

麻煩各位大大幫忙小弟如何解決與並還要安裝什麼語言才可測試..

此外eclipse測試下列三種都會有bug:
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
不知eclipse還要安裝什麼檔案才可執行

檔案名稱 程式錯誤畫面.rar
描述 執行畫面
檔案大小 540 Kbytes
下載次數 3 次
[Disk] 下載


發表人: andowson, 七段學員
2011-01-21 19:14:43
就您所提供之圖片來看,基本上程式碼應該沒什麼錯誤,該加入到CLASSPATH的jar檔也都加了,還是有這種找不到Class的問題,我也不清楚原因了。

如果硬要死馬要當活馬醫的話,我的建議就是重新再開一個新的Java Project,然後把程式碼先貼到記事本,再貼過去,再試試看。




會員註冊 / 登入  |  電腦版  |  Jump to top of page