java - error message : stream closed -


upon running following code under class flightsearch

string moresearch = "y";     list<flight> resultlist;      // load initial flight data db     if (!init()) {         return;     }     // background thread monitor changes in csv repository     filelistner fl = new filelistner();     fl.start();      {         inputstreamreader isr = new inputstreamreader(system.in);         bufferedreader br = new bufferedreader(isr);         // main thread gets input         input inputquery = new input();         try {             inputquery.getquery();         } catch (invalidexception e1) {             e1.getmessage();         } {             fl.stopthread();         }         // main thread starts processing task background monitors csv         // repo         queryprocessor processor = new queryprocessor();         resultlist = null;         resultlist = processor.matchquery(inputquery);          displayresult(resultlist, inputquery.getflightclass());          system.out                 .println("more flight query ? press n/n exit. anyother key continue searching.");          try {             moresearch = br.readline(); // line 56         } catch (ioexception e) {             // todo auto-generated catch block             e.printstacktrace();         }finally{             if(br!=null){                 try {                     br.close();                 } catch (ioexception e) {                     // todo auto-generated catch block                     e.printstacktrace();                 }             }         }       } while (!moresearch.equalsignorecase("n"));      system.out.println("thank !!!"); 

i following error :

java.io.ioexception: stream closed @ java.io.bufferedinputstream.getbufifopen(bufferedinputstream.java:162) @ java.io.bufferedinputstream.read(bufferedinputstream.java:325) @ sun.nio.cs.streamdecoder.readbytes(streamdecoder.java:283) @ sun.nio.cs.streamdecoder.implread(streamdecoder.java:325) @ sun.nio.cs.streamdecoder.read(streamdecoder.java:177) @ java.io.inputstreamreader.read(inputstreamreader.java:184) @ java.io.bufferedreader.fill(bufferedreader.java:154) @ java.io.bufferedreader.readline(bufferedreader.java:317) @ java.io.bufferedreader.readline(bufferedreader.java:382) @ com.myapp.flightsearch.main(flightsearch.java:56) 

i have tried moving

inputstreamreader isr = new inputstreamreader(system.in); bufferedreader br = new bufferedreader(isr); 

moving out of do-while loop in vain.

the problem

the problem execute br.close() that, javadoc states, closes stream and releases system resources associated it.

just quick verification comment out the:

if (br != null) { //    try { //        br.close(); //    } catch (ioexception e) { //        // todo auto-generated catch block //        e.printstacktrace(); //    } } 

and can answer s times want without exception.

a solution

a solution closing buffer reader after reads terminated:

    string moresearch = null;     inputstreamreader isr = new inputstreamreader(system.in);     bufferedreader br = new bufferedreader(isr);     try {         {             // ...             system.out.println("more flight query ? press n/n exit. anyother key continue searching.");             moresearch = br.readline();         } while (!moresearch.equalsignorecase("n"));      } catch (ioexception e) {         logger.getlogger(flightsearch.class.getname()).log(level.severe, "cant read line system.in based bufferedreader", e);     } {         if (br != null) {             try {                 br.close();             } catch (ioexception ignoreme) {                 logger.getlogger(flightsearch.class.getname()).log(level.severe, "can't close system.in based bufferedreader", ignoreme);             }         }     } 

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -