/** * @param args */ public static void main(String[] args)throws Exception { // TODO Auto-generated method stub //被索引文件所在目录 File fileDir = new File("F:\\Works\\JWork\\LuceneTest\\soucefile");
//索引文件存放目录 File indexDir = new File("indexfile");
Analyzer luceneAnalyzer = new StandardAnalyzer(); IndexWriter indexWriter = new IndexWriter(indexDir,luceneAnalyzer,true); File[] textFiles = fileDir.listFiles(); long startTime = new Date().getTime();
for(int i =0 ; i < textFiles.length; i++) { if(textFiles[i].isFile() && textFiles[i].getName().endsWith(".txt")) { System.out.println("File "+ textFiles[i].getCanonicalPath() + " is being indexed !"); Reader textReader = new FileReader(textFiles[i]); //新建一个文档 Document document = new Document(); //文档中放入内容字段 document.add(new Field("content",textReader));; //文档中放入路径字段 document.add(new Field("path",textFiles[i].getPath(),Field.Store.YES,Field.Index.TOKENIZED));