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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
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()在關閉輸出流前會調用一次flush()。效果如下:
如果flush()沒有被注釋掉,則則打印完“hello”之后會立即打印”writer start”。
總結
以上就是本文關于io中flush()函數的使用代碼示例的全部內容,希望對大家有所幫助。感興趣的朋友可以繼續參閱本站其他相關專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
原文鏈接:http://blog.csdn.net/zhhtao89/article/details/50127851