Upon running following code under class FlightSearch
String moreSearch = "y";
List<Flight> resultList;
// load initial flight data into DB
if (!init()) {
return;
}
// A background thread to monitor changes in csv repository
FileListner fl = new FileListner();
fl.start();
do {
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();
} finally {
fl.stopThread();
}
// main thread STARTs processing task as 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 to exit. Anyother key to 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 You !!!");
I get following error :
java.io.IOException: Stream closed
at java.io.BufferedInputStream.getBufIfOpen(BufferedInputStream.java:162)
at java.io.BufferedInputStream.read(BufferedInputStream.java:325)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:283)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:325)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:177)
at java.io.InputStreamReader.read(InputStreamReader.java:184)
at java.io.BufferedReader.fill(BufferedReader.java:154)
at java.io.BufferedReader.readLine(BufferedReader.java:317)
at java.io.BufferedReader.readLine(BufferedReader.java:382)
at com.myApp.FlightSearch.main(FlightSearch.java:56)
I have also tried moving
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
moving out of do-while loop but in vain.
Your issue is that you're calling
br.close()
in thefinally
block where you read a line from the buffer. When you close aStream
, any stream that was wrapped by the one you closed is also closed. As a result, your first iteration through thedo { } while ()
loop is closing theSystem.in InputStream
, which you cannot then reopen.To avoid this, you can use a
CloseShieldInputStream
from Apache Commons to wrapSystem.in
before you wrap it with theInputStreamReader
. This will allow you to close theBufferedReader
(which will also close theInputStreamReader
and theCloseShieldInputStream
) without triggering the closure ofSystem.in
.See bmargulies answer here: Closing BufferedReader and System.in
After making changes as suggested in previous answer my code didn't tun but with slight more changes as shown below, my code compiled and run just fine
thanks for help.
The problem
The problem is that you execute the
br.close()
that, as javadoc states, closes the stream and releases any system resources associated with it.Just for a quick verification comment out the:
and you can answer
s
any times you want without any exception.A solution
A solution is closing the buffer reader after all reads are terminated: