error message : stream closed

后端 未结 3 849
遇见更好的自我
遇见更好的自我 2020-12-21 19:44

Upon running following code under class FlightSearch

String moreSearch = \"y\";
    List resultList;

    // load initial flight d         


        
3条回答
  •  感动是毒
    2020-12-21 20:17

    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);
                }
            }
        }
    

提交回复
热议问题