从 BufferedReader 读取会抛出异常,因为在创建新的 BufferedReader 后流关闭
Reading from BufferedReader throws exception as stream closed after creating a new BufferedReader
以下是一种方法,它接受用户的输入和 returns 响应。
private String getValueFromUser(String propertyValue){
String response = "";
try(BufferedReader br = new BufferedReader(new InputStreamReader(System.in))){
response = br.readLine();
if (response.equals("")){
return propertyValue;
}
} catch (IOException e) {
e.printStackTrace();
}
return response;
}
此方法在第一次调用时运行良好,但在后续调用中抛出以下异常。
java.io.IOException: Stream closed
at java.io.BufferedInputStream.getBufIfOpen(BufferedInputStream.java:170)
at java.io.BufferedInputStream.read(BufferedInputStream.java:336)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
at java.io.InputStreamReader.read(InputStreamReader.java:184)
at java.io.BufferedReader.fill(BufferedReader.java:161)
at java.io.BufferedReader.readLine(BufferedReader.java:324)
at java.io.BufferedReader.readLine(BufferedReader.java:389)
at com.ripple.utility.ConfigurationUtility.getValueFromUser(ConfigurationUtility.java:119)
at com.ripple.utility.ConfigurationUtility.createPropertiesDTO(ConfigurationUtility.java:108)
at com.ripple.utility.ConfigurationUtility.setProperties(ConfigurationUtility.java:169)
at com.ripple.utility.ConfigurationUtility.main(ConfigurationUtility.java:191)
有人可以帮忙解释一下代码的问题吗?
当 BufferedReader
在 try-with-resources 块结束时关闭时,System.in
也关闭并且无法重新打开。
您应该打开 reader 一次,并在您可以使用它的地方保持打开状态,直到您的程序完成。
以下是一种方法,它接受用户的输入和 returns 响应。
private String getValueFromUser(String propertyValue){
String response = "";
try(BufferedReader br = new BufferedReader(new InputStreamReader(System.in))){
response = br.readLine();
if (response.equals("")){
return propertyValue;
}
} catch (IOException e) {
e.printStackTrace();
}
return response;
}
此方法在第一次调用时运行良好,但在后续调用中抛出以下异常。
java.io.IOException: Stream closed
at java.io.BufferedInputStream.getBufIfOpen(BufferedInputStream.java:170)
at java.io.BufferedInputStream.read(BufferedInputStream.java:336)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
at java.io.InputStreamReader.read(InputStreamReader.java:184)
at java.io.BufferedReader.fill(BufferedReader.java:161)
at java.io.BufferedReader.readLine(BufferedReader.java:324)
at java.io.BufferedReader.readLine(BufferedReader.java:389)
at com.ripple.utility.ConfigurationUtility.getValueFromUser(ConfigurationUtility.java:119)
at com.ripple.utility.ConfigurationUtility.createPropertiesDTO(ConfigurationUtility.java:108)
at com.ripple.utility.ConfigurationUtility.setProperties(ConfigurationUtility.java:169)
at com.ripple.utility.ConfigurationUtility.main(ConfigurationUtility.java:191)
有人可以帮忙解释一下代码的问题吗?
当 BufferedReader
在 try-with-resources 块结束时关闭时,System.in
也关闭并且无法重新打开。
您应该打开 reader 一次,并在您可以使用它的地方保持打开状态,直到您的程序完成。