* author lighter date 2006-8-7 */ public class LuceneExample { public static void main(String[] args) throws Exception { File fileDir = new File("c:\\s"); // 指明要索引文件夹的位置,这里是C盘的S文件夹下 File indexDir = new File("c:\\index"); // 这里放索引文件的位置 File[] textFiles = fileDir.listFiles(); Analyzer luceneAnalyzer = new StandardAnalyzer(); IndexWriter indexWriter = new IndexWriter(indexDir,luceneAnalyzer,true); indexFile(luceneAnalyzer,indexWriter, textFiles); indexWriter.optimize();//optimize()方法是对索引进行优化 indexWriter.close(); } public static void indexFile(Analyzer luceneAnalyzer,IndexWriter indexWriter,File[] textFiles) throws Exception { //增加document到索引去 for (int i = 0; i < textFiles.length; i++) { if (textFiles[i].isFile() && textFiles[i].getName().endsWith(".txt")) { String temp = FileReaderAll(textFiles[i].getCanonicalPath(),"GBK"); Document document = new Document(); Field FieldBody = new Field("body", temp, Field.Store.YES,Field.Index.TOKENIZED); 数据挖掘研究院 document.add(FieldBody); indexWriter.addDocument(document); } } } public static String FileReaderAll(String FileName, String charset)throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader( new FileInputStream(FileName), charset)); String line = ""; String temp = "";
|