欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

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

时间:2021-07-26 07:52:32|栏目:JAVA代码|点击:

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()函数的使用代码示例的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

上一篇:Java try-with-resource语法使用解析

栏    目:JAVA代码

下一篇:Java基础之类型封装器示例

本文标题:IO中flush()函数的使用代码示例

本文地址:http://www.codeinn.net/misctech/161411.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有