有人能解釋爲什麼我們會以更簡單的方式使用system.out.flush()
?如果有可能丟失數據,請給我一個例子。如果你在下面的代碼中評論它,沒有什麼改變!爲什麼我們使用system.out.flush()?
class ReverseApp{
public static void main(String[] args) throws IOException{
String input, output;
while(true){
System.out.print("Enter a string: ");
System.out.flush();
input = getString(); // read a string from kbd
if(input.equals("")) // quit if [Enter]
break;
// make a Reverser
Reverser theReverser = new Reverser(input);
output = theReverser.doRev(); // use it
System.out.println("Reversed: " + output);
}
}
}
謝謝
默認情況下'PrintStream'的某些方法不會'flush'。 –
http://stackoverflow.com/questions/7166328/when-why-to-call-system-out-flush-in-java – DT7