這篇文章主要講解了IO中如何使用flush()函數(shù),內(nèi)容清晰明了,對(duì)此有興趣的小伙伴可以學(xué)習(xí)一下,相信大家閱讀完之后會(huì)有幫助。
榕城ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場(chǎng)景,ssl證書未來(lái)市場(chǎng)廣闊!成為成都創(chuàng)新互聯(lián)的ssl證書銷售渠道,可以享受市場(chǎng)價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18980820575(備注:SSL證書合作)期待與您的合作!
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”。
看完上述內(nèi)容,是不是對(duì)IO中如何使用flush()函數(shù)有進(jìn)一步的了解,如果還想學(xué)習(xí)更多內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。