Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
466 views
in Technique[技术] by (71.8m points)

java - error message : stream closed

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:

if (br != null) {
//    try {
//        br.close();
//    } catch (IOException e) {
//        // TODO Auto-generated catch block
//        e.printStackTrace();
//    }
}

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:

    String moreSearch = null;
    InputStreamReader isr = new InputStreamReader(System.in);
    BufferedReader br = new BufferedReader(isr);
    try {
        do {
            // ...
            System.out.println("More Flight Query ? Press n/N to exit. Anyother key to continue searching.");
            moreSearch = br.readLine();
        } while (!moreSearch.equalsIgnoreCase("n"));

    } catch (IOException e) {
        Logger.getLogger(FlightSearch.class.getName()).log(Level.SEVERE, "Cant read line from a System.in based BufferedReader", e);
    } finally {
        if (br != null) {
            try {
                br.close();
            } catch (IOException ignoreMe) {
                Logger.getLogger(FlightSearch.class.getName()).log(Level.SEVERE, "Can't close a System.in based BufferedReader", ignoreMe);
            }
        }
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...