我正在尝试使用 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
它只是循环运行。我肯定错过了什么。
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;
}
这一切似乎都是关于 commandLine() 不起作用,所以我刚刚包含了它和 main。