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()在關(guān)閉輸出流前會調(diào)用一次flush()。效果如下:

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

總結(jié)
以上就是本文關(guān)于IO中flush()函數(shù)的使用代碼示例的全部內(nèi)容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
相關(guān)文章
Java接口回調(diào)和方法回調(diào)的簡單實現(xiàn)步驟
這篇文章主要介紹了Java接口回調(diào)和方法回調(diào)的相關(guān)資料,接口回調(diào)是一種設(shè)計模式,實現(xiàn)三方解耦,調(diào)用者提供接口實現(xiàn),文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2025-03-03
Spring?Boot中自動執(zhí)行sql腳本的方法實例
在SpringBoot的架構(gòu)中,DataSourceInitializer類可以在項目啟動后初始化數(shù)據(jù),我們可以通過自動執(zhí)行自定義sql腳本初始化數(shù)據(jù),下面這篇文章主要給大家介紹了關(guān)于Spring?Boot中自動執(zhí)行sql腳本的相關(guān)資料,需要的朋友可以參考下2022-01-01
淺談Java實體對象的三種狀態(tài)以及轉(zhuǎn)換關(guān)系
這篇文章主要介紹了淺談Java實體對象的三種狀態(tài)以及轉(zhuǎn)換關(guān)系,具有一定參考價值,需要的朋友可以,看看。。2017-11-11
在java中實現(xiàn)C#語法里的按引用傳遞參數(shù)的方法
下面小編就為大家?guī)硪黄趈ava中實現(xiàn)C#語法里的按引用傳遞參數(shù)的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-09-09

