java 来自 System.in 的 BufferedReader 输入尝试抛出异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17174752/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
BufferedReader input attempt from System.in throwing exceptions
提问by John Tate
I am trying to read a from the input using BufferedReader. It works the first time but the second time it is ran I get an exception.
我正在尝试使用 BufferedReader 从输入中读取 a。它第一次运行,但第二次运行时出现异常。
john@fekete:~/devel/java/pricecalc$ java frontend.CUI
> gsdfgd
Invalid command!
> I/O Error getting string: java.io.IOException: Stream closed
I/O Error: java.io.IOException: java.io.IOException: Stream closed
> I/O Error getting string: java.io.IOException: Stream closed
I/O Error: java.io.IOException: java.io.IOException: Stream closed
> I/O Error getting string: java.io.IOException: Stream closed
It just keeps running with that in a loop. I must have missed something.
它只是在循环中继续运行。我一定错过了什么。
public static void main(String args[]) {
if (args.length == 0) {
while (!exit) {
try {
exit = processLine(commandLine());
} catch (IOException e) {
System.out.println("I/O Error: " + e);
}
}
System.out.println("Bye!");
} else if (args.length == 1) {
String line = new String(args[0]);
processLine(line);
} else {
String line = new String(args[0]);
for (String np : args) {
line = new String(line + " " + np);
}
processLine(line);
}
}
static private String commandLine() throws IOException {
String str = new String();
try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
System.out.print("> ");
str = new String(br.readLine());
str = str.trim();
} catch (IOException e) {
System.out.println("I/O Error getting string: "+ str + " " + e);
throw new IOException(e);
}
return str;
}
It really all seems to be about commandLine() not working so I've just included that and main.
这一切似乎都是关于 commandLine() 不起作用,所以我刚刚包括了那个和主要的。
回答by Jon Skeet
Yes, you're closing the stream here:
是的,您正在此处关闭流:
try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in)))
That try-with-resources statement will close the BufferedReader
at the end of the block, which will close the InputStreamReader
, which will close System.in
.
该 try-with-resources 语句将BufferedReader
在块的末尾关闭InputStreamReader
,这将关闭 ,这将关闭System.in
。
You don't want to do that in this case, so just use:
在这种情况下你不想这样做,所以只需使用:
// Deliberately not closing System.in!
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try {
...
}
It's still possible that this won't quite behave as you want though, as the BufferedReader
could potentially consume (and buffer) more data. You'd be better off creating the BufferedReader
once(in the calling code) and passing it to the method.
尽管如此,这仍然可能不会像您想要的那样表现,因为它BufferedReader
可能会消耗(和缓冲)更多数据。您最好创建BufferedReader
一次(在调用代码中)并将其传递给方法。
Oh, and I suggest you get rid of this:
哦,我建议你摆脱这个:
String str = new String();
There's no need for it at all. This would be better:
根本没有必要。这样会更好:
String str = "";
But even then, it's a pointless assignment. Likewise you don't need to create a new String from the one returned by readLine()
. Just use:
但即便如此,这也是一项毫无意义的任务。同样,您不需要从返回的字符串创建一个新字符串readLine()
。只需使用:
return br.readLine().trim();
... within the try
block. Also, there's no point in logging str
within the catch
block, as it's going to be empty - the IOException
will only be thrown when reading the line...
...在try
街区内。此外,str
在catch
块内进行日志记录是没有意义的,因为它将是空的 -IOException
只有在阅读该行时才会抛出......