服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - IO中flush()函数的使用代码示例

IO中flush()函数的使用代码示例

2021-03-17 13:30御风逍遥 Java教程

这篇文章主要介绍了IO中flush()函数的使用代码示例,具有一定借鉴价值,需要的朋友可以参考下

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()。效果如下:

IO中flush()函数的使用代码示例

如果flush()没有被注释掉,则则打印完“hello”之后会立即打印”writer start”。

IO中flush()函数的使用代码示例

总结

以上就是本文关于io中flush()函数的使用代码示例的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

原文链接:http://blog.csdn.net/zhhtao89/article/details/50127851

延伸 · 阅读

精彩推荐