软件编程
位置:首页>> 软件编程>> java编程>> IO中flush()函数的使用代码示例

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

作者:御风逍遥  发布时间:2023-11-27 03:49:00 

标签: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.


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()函数的使用代码示例

来源:http://blog.csdn.net/zhhtao89/article/details/50127851

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com