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秒才會(huì)打印”writer start”,”writer close”,因?yàn)閣riter.close()在關(guān)閉輸出流前會(huì)調(diào)用一次flush()。效果如下:
如果flush()沒(méi)有被注釋掉,則則打印完“hello”之后會(huì)立即打印”writer start”。
總結(jié)
以上就是本文關(guān)于IO中flush()函數(shù)的使用代碼示例的全部?jī)?nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!
相關(guān)文章
Java設(shè)計(jì)模式之抽象工廠模式實(shí)例詳解
這篇文章主要介紹了Java設(shè)計(jì)模式之抽象工廠模式,結(jié)合實(shí)例形式分析了抽象工廠模式的概念、功能、定義與使用方法,需要的朋友可以參考下2017-09-09Java web含驗(yàn)證碼及權(quán)限登錄實(shí)例代碼
這篇文章主要介紹了Java web含驗(yàn)證碼及權(quán)限登錄實(shí)例代碼,所用到的開(kāi)發(fā)工具為myeclipse10,MySQL數(shù)據(jù)庫(kù),具體實(shí)現(xiàn)代碼大家參考下本文吧2017-03-03SpringCloud Gateway的基本入門和注意點(diǎn)詳解
這篇文章主要介紹了SpringCloud Gateway的基本入門和注意點(diǎn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10Java通過(guò)FTP服務(wù)器上傳下載文件的方法
本文介紹了如何使用Apache Jakarta Commons Net(commons-net-3.3.jar)基于FileZilla Server服務(wù)器實(shí)現(xiàn)FTP服務(wù)器上文件的上傳/下載/刪除等操作,需要的朋友可以參考下2015-07-07IDEA2020.3創(chuàng)建web工程的完整步驟
這篇文章主要給大家介紹了關(guān)于IDEA2020.3創(chuàng)建web工程的完整步驟,文中通過(guò)圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01深入學(xué)習(xí)Spring Cloud-Ribbon
這篇文章主要介紹了Spring Cloud-Ribbon的相關(guān)知識(shí),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友一起看看吧2021-03-03Java cookie和session會(huì)話技術(shù)介紹
session的工作原理和cookie非常類似,在cookie中存放一個(gè)sessionID,真實(shí)的數(shù)據(jù)存放在服務(wù)器端,客戶端每次發(fā)送請(qǐng)求的時(shí)候帶上sessionID,服務(wù)端根據(jù)sessionID進(jìn)行數(shù)據(jù)的響應(yīng)2023-04-04