第二次添加时,流关闭异常

时间:2013-09-01 09:51:58

标签: java file lucene stream buffer

我正在尝试使用Lucene索引文档...但是我收到StreamClosed异常..

我认为它与Java相关,然后是Lucene ......

有人可以指导.. 代码段如下:

 static void indexDocs(File file,boolean flag,Directory dir,IndexWriterConfig iwc)
  throws IOException {
  // do not try to index files that cannot be read

      FileInputStream fis = null;
 if (file.canRead()) {

    if (file.isDirectory()) 
    {

     String[] files = file.list();
     System.out.println("list "  + files.length);
      if (files != null) {
        for (int i = 0; i < files.length; i++) {
            System.out.println("Invoked for  "  +  i  +  "and  "  +   files[i]);            
          indexDocs(new File(file, files[i]),flag,dir,iwc);
        }
      }
   } 
    else {

        boolean flags=true;

      try {
        fis = new FileInputStream(file);

     } catch (FileNotFoundException fnfe) {

       fnfe.printStackTrace();
     }
      try {
          Document doc = new Document();         

          LineNumberReader lnr=new LineNumberReader(new FileReader(file));

          Field pathField = new StringField("path", file.getPath(), Field.Store.YES);
          doc.add(pathField);


          String line=null;
          int i=0;      

          doc.add(new StringField("TT",file.getName(),Field.Store.YES)); 

          BufferedReader br=new BufferedReader(new InputStreamReader(fis));

           doc.add(new TextField("DD", br));

           System.out.println("Looping Again");

          while(flags)
          {                      
              IndexWriter iwcTemp1=new IndexWriter(dir,iwc);

          while( null != (line = lnr.readLine()) ){  

            i++;   

          StringField sf=new StringField("EEE",line.trim(),Field.Store.YES);
            doc.add(sf);

            if(i%10000==0)
            {      
                System.out.println("Breaking   " +  i);
                lnr.mark(i);
                break;
            }               
            sf=null;
          }        
          if(line==null)
          {
              System.out.println("FALSE "   );
              flags=false;
          }               

          System.out.println("FILE NAME IS FTP "  +   file.getName());


          if (iwcTemp1.getConfig().getOpenMode() == OpenMode.CREATE_OR_APPEND) {

              try
              {               
                iwcTemp1.addDocument(doc);
                iwcTemp1.commit();                                      
                iwcTemp1.close();

              }catch(Throwable t)
              {
                  lnr.close();
                    br.close();
                    fis.close(); 
                  t.printStackTrace();
              }

          } else {
              try
              {

            System.out.println("updating " + file);
            iwcTemp1.updateDocument(new Term("path", file.getPath()), doc);
              }catch(Exception e)
              {
                  e.printStackTrace();
              }
          }        
          System.out.println("END OF WHILE");
          lnr.reset();
          }//end of While 
      }catch (Exception e) {
         e.printStackTrace();
      }finally {
       fis.close();
      }
    }
  }
}

我得到的例外是在我将文档添加到Writer的行上... 异常跟踪:

java.io.IOException: Stream closed
        at java.io.BufferedReader.ensureOpen(BufferedReader.java:114)
        at java.io.BufferedReader.read(BufferedReader.java:270)
        at org.apache.lucene.analysis.standard.StandardTokenizerImpl.zzRefill(StandardTokenizerImpl.java:923)
        at org.apache.lucene.analysis.standard.StandardTokenizerImpl.getNextToken(StandardTokenizerImpl.java:1133)
        at org.apache.lucene.analysis.standard.StandardTokenizer.incrementToken(StandardTokenizer.java:171)
        at org.apache.lucene.analysis.standard.StandardFilter.incrementToken(StandardFilter.java:49)
        at org.apache.lucene.index.DocInverterPerField.processFields(DocInverterPerField.java:102)
        at org.apache.lucene.index.DocFieldProcessor.processDocument(DocFieldProcessor.java:245)
        at org.apache.lucene.index.DocumentsWriterPerThread.updateDocument(DocumentsWriterPerThread.java:265)
        at org.apache.lucene.index.DocumentsWriter.updateDocument(DocumentsWriter.java:432)
        at org.apache.lucene.index.IndexWriter.updateDocument(IndexWriter.java:1513)
        at org.apache.lucene.index.IndexWriter.addDocument(IndexWriter.java:1188)
        at org.apache.lucene.index.IndexWriter.addDocument(IndexWriter.java:1169)
        at com.rancore.MainClass2.indexDocs(MainClass2.java:236)

有人可以指导......我哪里错了...请指导......

1 个答案:

答案 0 :(得分:0)

您的异常处理结构不正确。如果新的FileInputStream()抛出异常,则不应该继续读取代码。