IO中flush()函数的使用代码示例
程序员文章站
2023-12-18 16:53:52
the java.io.writer.flush() method flushes the stream. if the stream has saved any char...
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()在关闭输出流前会调用一次flush()。效果如下:
如果flush()没有被注释掉,则则打印完“hello”之后会立即打印”writer start”。
总结
以上就是本文关于io中flush()函数的使用代码示例的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!