package lucene.test.index; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; //import java.io.FileReader; import java.io.IOException; import java.util.Date; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriter.MaxFieldLength; import org.apache.lucene.store.Directory; import org.apache.lucene.store.FSDirectory; public class IndexFileManager { private static String IndexPath="D://MajorSoft//lucene//temp//IndexPath"; private static String FilePath="D://MajorSoft//lucene//temp//FilePath//Test.txt"; public static void main(String args[]){ Date start=new Date(); File file=new File(FilePath); try { //String dirName=indexPath; Directory dir=FSDirectory.getDirectory(IndexPath); Analyzer analyzer=new StandardAnalyzer(); IndexWriter writer=new IndexWriter(dir,analyzer,false,MaxFieldLength.UNLIMITED); Document document=new Document(); Field field_name=new Field("filename",file.getName(),Field.Store.YES,Field.Index.NOT_ANALYZED); document.add(field_name); FileInputStream inputFile=new FileInputStream(file); int len=inputFile.available(); byte[] buffer=new byte[len]; inputFile.read(buffer); inputFile.close(); String content=new String(buffer); Field field_content=new Field("content",content,Field.Store.YES,Field.Index.ANALYZED); document.add(field_content); writer.addDocument(document); writer.optimize(); writer.close(); Date end=new Date(); long indexTime=end.getTime()-start.getTime(); System.out.println("完成所有共需:(ms)"); System.out.println(indexTime); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }