error message : stream closed

后端 未结 3 850
遇见更好的自我
遇见更好的自我 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:32

    Your issue is that you're calling br.close() in the finally block where you read a line from the buffer. When you close a Stream, any stream that was wrapped by the one you closed is also closed. As a result, your first iteration through the do { } while () loop is closing the System.in InputStream, which you cannot then reopen.

    To avoid this, you can use a CloseShieldInputStream from Apache Commons to wrap System.in before you wrap it with the InputStreamReader. This will allow you to close the BufferedReader (which will also close the InputStreamReader and the CloseShieldInputStream) without triggering the closure of System.in.

    See bmargulies answer here: Closing BufferedReader and System.in

提交回复
热议问题