0
我想從使用BufferedReader的輸入中讀取一個。它第一次運作,但第二次運行我得到一個異常。從System.in BufferedReader輸入嘗試拋出異常
[email protected]:~/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。
在哪裏定義了exit? –
請發佈一個可編輯的程序。 –
@RohitJain它的Java 7 – sanbhat