我试图从控制台读取文本行.线数预先不知道. 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("~");
}