我正在尝试从控制台读取文本行。行数事先不知道。BufferedReader.readLine() 方法读取一行,但在最后一行之后它等待来自控制台的输入。应该怎么做才能避免这种情况?
请看下面的代码片段:
public static String[] getLinesFromConsole() {
String strLine = "";
try {
// Get the object of DataInputStream
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String line = "";
while ((line = br.readLine()) != null)
strLine += line + "~"; //edited
isr.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
return strLine.split("~");
}