IO中flush()函數(shù)的使用代碼示例
The java.io.Writer.flush() method flushes the stream. If the stream has saved any characters from the various write() methods in a buffer, write them immediately to their intended destination. Then, if that destination is another character or byte stream, flush it. Thus one flush() invocation will flush all the buffers in a chain of Writers and OutputStreams.
public class Demo {
public static void main(String[] ars) throws Exception {
System.out.println("hello");
PrintWriter writer = new PrintWriter(System.out);
writer.println("writer start");
// writer.flush();
try {
Thread.sleep(3000);
}
catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
writer.println("writer close");
writer.close();
}
}
如上面代碼,如果flush()被注釋掉,則打印完“hello”之后3秒才會打印”writer start”,”writer close”,因為writer.close()在關閉輸出流前會調(diào)用一次flush()。效果如下:

如果flush()沒有被注釋掉,則則打印完“hello”之后會立即打印”writer start”。

總結(jié)
以上就是本文關于IO中flush()函數(shù)的使用代碼示例的全部內(nèi)容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
相關文章
SpringCloud Gateway的基本入門和注意點詳解
這篇文章主要介紹了SpringCloud Gateway的基本入門和注意點,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10
IDEA2020.3創(chuàng)建web工程的完整步驟
這篇文章主要給大家介紹了關于IDEA2020.3創(chuàng)建web工程的完整步驟,文中通過圖文介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-01-01
Java cookie和session會話技術(shù)介紹
session的工作原理和cookie非常類似,在cookie中存放一個sessionID,真實的數(shù)據(jù)存放在服務器端,客戶端每次發(fā)送請求的時候帶上sessionID,服務端根據(jù)sessionID進行數(shù)據(jù)的響應2023-04-04

