我正在使用这个自定义类来模拟system.console
eclipse 中的 while 调试。让我感到困惑的是,reader = new BufferedReader(new InputStreamReader(
System.in));
在第二次调用 of 之后返回 null readLine
。
我以为是因为我关闭了System.in,但是根据oracle
InputStream 的 close 方法什么也不做。
所以现在我很难过。这是代码:
public class CustomConsole {
public String readLine(String format, Object... args) {
if (System.console() != null) {
return System.console().readLine(format, args);
}
BufferedReader reader = null;
String line = null;
try {
System.out.print(String.format(format, args));
reader = new BufferedReader(new InputStreamReader(
System.in));
line = reader.readLine();
} catch (IOException e) {
Logger.fatal(e.getMessage());
System.exit(-1);
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
Logger.error("CustomConsole.readLine(): BufferedReader could not be closed");
e.printStackTrace();
}
} else {
Logger.error("CustomConsole.readLine(): BufferedReader is null");
}
}
return line;
}
public String readLine() {
return readLine("", "");
}
public char[] readPassword(String format, Object... args) {
if (System.console() != null) return System.console().readPassword(format, args);
return this.readLine(format, args).toCharArray();
}
}