我尝试使用System.readline()流从控制台输入读取。 我希望该方法检查是否已将一行插入控制台,如果是,则检查它是否等于"退出"。如果一行未插入控制台,我希望该方法什么都不做。
/**
* this method checks if the Exit command with written on the Servers console
*/
private void checkIfTypedExitOnKeyboard() {
System.out.println("checking if user printed Exit:-)");//DELETE TEST
long end=System.currentTimeMillis()+2000;
InputStreamReader fileInputStream=new InputStreamReader(System.in);
BufferedReader bufferedReader=new BufferedReader(fileInputStream);
try
{
while(System.currentTimeMillis()<end && bufferedReader.ready() && (bufferedReader.readLine()!=null))
{
if (bufferedReader.ready() && fileInputStream.ready() && bufferedReader.readLine().equals("Exit")){//TODO fix this
System.out.println("Reactor is shutting down");
stopReactor();
}
}
bufferedReader.close();
}
catch(java.io.IOException e)
{
e.printStackTrace();
}
}
为什么这段代码不起作用?
感谢: - )