Index: IndexFiles.java =================================================================== RCS file: /home/cvspublic/jakarta-lucene/src/demo/org/apache/lucene/demo/IndexFiles.java,v retrieving revision 1.1 diff -u -r1.1 IndexFiles.java --- IndexFiles.java 26 Jan 2002 15:01:31 -0000 1.1 +++ IndexFiles.java 24 Jan 2004 18:04:05 -0000 @@ -3,7 +3,7 @@ /* ==================================================================== * The Apache Software License, Version 1.1 * - * Copyright (c) 2001 The Apache Software Foundation. All rights + * Copyright (c) 2001,2004 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without @@ -54,43 +54,58 @@ * . */ +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.Date; + import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.index.IndexWriter; -import java.io.File; -import java.util.Date; +/* + * Index all files found in a directory and all its sub-directories + * + */ +public class IndexFiles { + + public static void main(String[] args) throws IOException { + Date start = new Date(); -class IndexFiles { - public static void main(String[] args) { - try { - Date start = new Date(); - - IndexWriter writer = new IndexWriter("index", new StandardAnalyzer(), true); - indexDocs(writer, new File(args[0])); - - writer.optimize(); - writer.close(); - - Date end = new Date(); - - System.out.print(end.getTime() - start.getTime()); - System.out.println(" total milliseconds"); - - } catch (Exception e) { - System.out.println(" caught a " + e.getClass() + - "\n with message: " + e.getMessage()); - } - } - - public static void indexDocs(IndexWriter writer, File file) - throws Exception { - if (file.isDirectory()) { - String[] files = file.list(); - for (int i = 0; i < files.length; i++) - indexDocs(writer, new File(file, files[i])); - } else { - System.out.println("adding " + file); - writer.addDocument(FileDocument.Document(file)); - } - } -} + IndexWriter writer = + new IndexWriter("index", new StandardAnalyzer(), true); + indexDocs(writer, new File(args[0])); + + writer.optimize(); + writer.close(); + + Date end = new Date(); + + System.out.print(end.getTime() - start.getTime()); + System.out.println(" total milliseconds"); + } + + private static void indexDocs(IndexWriter writer, File file) + throws IOException { + //do not try to index files that cannot be read + if (file.canRead()) { + if (file.isDirectory()) { + String[] files = file.list(); + //an IO error could occur + if (files != null) { + for (int i = 0; i < files.length; i++) + indexDocs(writer, new File(file, files[i])); + } + } else { + System.out.println("adding " + file); + try { + writer.addDocument(FileDocument.Document(file)); + } + // at least on windows, some temporary files raise this exception with an "access denied" message + // checking if the file can be read doesn't help + catch (FileNotFoundException fnfe) { + ; + } + } + } + } +} \ No newline at end of file