1、创建Directory
Directory directory=FSDirectory.open(new File("D:/eclipse-jee-mars-1-win32-x86_64/lucenefile/index01"));
2、创建IndexWriter
IndexWriterConfig indexWriterConfig=new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35));
IndexWriter writer=null;
writer=new IndexWriter(directory, indexWriterConfig);
3、创建Document
document=new Document();
4、为Document添加Field
File file=new File("D:/eclipse-jee-mars-1-win32-x86_64/lucenefile");
for(File file2:file.listFiles()){
document.add(new Field("content", new FileReader(file2)));
document.add(new Field("filename", file2.getName(),Field.Store.YES,Field.Index.NOT_ANALYZED));
document.add(new Field("path", file2.getAbsolutePath(),Field.Store.YES , Field.Index.NOT_ANALYZED));
5、通过IndexWriter添加文档到索引中
writer.addDocument(document);}
注意:要在索引创建完成后将IndexWriter关闭