Hi,
     I don't know why the code file didn't reach the attachment, I am resending it; now as a simple text file instead of .java file.
May be it attaches now properly.
Thanks
Jitender
import java.io.*;
import org.apache.lucene.analysis.standard.*;
import org.apache.lucene.document.*;
import org.apache.lucene.index.*;
import org.apache.lucene.document.Field;

public class IndexDatanew{
protected static final String INDEX_FOLDER = "C:\\Temp\\DB_GT11111";
  public static void main(String[] args)
  {
        IndexWriter writ = null;
     try{
         System.out.println("Index Directory: " + args[0]);
         IndexDatanew objDBdex = new IndexDatanew();
         boolean createDex = !objDBdex.indexExists();
         writ = new IndexWriter(INDEX_FOLDER, new StandardAnalyzer(), createDex);
         indexDocs(writ, new File(args[0]));
          }catch(IOException ex) {
                   ex.printStackTrace();
   }catch(Exception ex) {
                   ex.printStackTrace();
   } finally {
           try {
                //   if (writ != null)
                // writ.close();                     //ACTUAL way to close IndexWriter 
           } catch (Exception e) {
                   e.printStackTrace();
           }
   }
 }

 public static void indexDocs(IndexWriter writ, File a) throws Exception {

    InputStream is = null;
    try{
           if (a.isDirectory()){
          File[] files = a.listFiles();
          System.out.println(files.length);
          for (int i=0; i< files.length; i++) {
                 System.out.println(((File)files[i]).getAbsolutePath());
                         indexDocs(writ, (File)files[i]);
          }
         
           }
         else{
             System.out.println("adding: " + a.getAbsolutePath());
              is = new FileInputStream(a);
                  Document doc = new Document();
              doc.add(Field.Keyword("path", a.getCanonicalPath()));
                  BufferedReader rdr = new BufferedReader((Reader)new 
InputStreamReader(is));
              StringBuffer fileBuffer = new StringBuffer();
              String line;

              while ((line = rdr.readLine()) != null ) {
                        fileBuffer.append(line);
                    }
                 StringReader ab = new StringReader(fileBuffer.toString());
              doc.add(Field.Text("body", (Reader)ab));
              System.out.println(a);
              writ.addDocument(doc);
              System.out.println(writ != null);
                  writ.close();                    // wrong way to close IndexWriter 
and causes exception too
          }

      }
         catch(Exception ex) {
               ex.printStackTrace();
           } finally {
            if (is != null)
                   is.close();
           }

  }

  public boolean indexExists(){
       return false;
     }

}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to