您好,登錄后才能下訂單哦!
這篇文章主要講解了IO中如何使用flush()函數(shù),內(nèi)容清晰明了,對(duì)此有興趣的小伙伴可以學(xué)習(xí)一下,相信大家閱讀完之后會(huì)有幫助。
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)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。