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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

御风逍遥   2021-03-28 我要评论
想了解IO中flush()函数的使用代码示例的相关内容吗,御风逍遥在本文为您仔细讲解IO中flush()函数的使用的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:java,io,flush,io流flush,java,io,字符流flush,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.

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

猜您喜欢

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们