1 package org.apache.lucene.demo; 2 3 18 19 import org.apache.lucene.analysis.standard.StandardAnalyzer; 20 import org.apache.lucene.index.IndexWriter; 21 22 import java.io.File ; 23 import java.io.FileNotFoundException ; 24 import java.io.IOException ; 25 import java.util.Date ; 26 27 class IndexFiles { 28 public static void main(String [] args) throws IOException { 29 String usage = "java " + IndexFiles.class + " <root_directory>"; 30 if (args.length == 0) { 31 System.err.println("Usage: " + usage); 32 System.exit(1); 33 } 34 35 Date start = new Date (); 36 try { 37 IndexWriter writer = new IndexWriter("index", new StandardAnalyzer(), true); 38 indexDocs(writer, new File (args[0])); 39 40 writer.optimize(); 41 writer.close(); 42 43 Date end = new Date (); 44 45 System.out.print(end.getTime() - start.getTime()); 46 System.out.println(" total milliseconds"); 47 48 } catch (IOException e) { 49 System.out.println(" caught a " + e.getClass() + 50 "\n with message: " + e.getMessage()); 51 } 52 } 53 54 public static void indexDocs(IndexWriter writer, File file) 55 throws IOException { 56 if (file.canRead()) { 58 if (file.isDirectory()) { 59 String [] files = file.list(); 60 if (files != null) { 62 for (int i = 0; i < files.length; i++) { 63 indexDocs(writer, new File (file, files[i])); 64 } 65 } 66 } else { 67 System.out.println("adding " + file); 68 try { 69 writer.addDocument(FileDocument.Document(file)); 70 } 71 catch (FileNotFoundException fnfe) { 74 ; 75 } 76 } 77 } 78 } 79 } 80 | Popular Tags |