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

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

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

标签:java,io,flush
0
投稿

猜你喜欢

  • java编程中字节流转换成字符流的实现方法

    2021-06-09 15:59:04
  • Unity实现文本转贴图

    2022-05-10 19:53:04
  • C#快速排序算法实例分析

    2023-06-17 00:30:00
  • java数据结构与算法之中缀表达式转为后缀表达式的方法

    2023-11-22 12:51:35
  • Java多线程的其他知识_动力节点Java学院整理

    2023-09-06 06:01:58
  • Android通过实现GridView的横向滚动实现仿京东秒杀效果

    2023-06-08 22:53:46
  • 解决nacos升级spring cloud 2020.0无法使用bootstrap.yml的问题

    2021-12-02 19:44:29
  • 深入学习java位运算的基础知识

    2021-06-01 07:10:40
  • Java并发编程之同步容器与并发容器详解

    2023-10-17 04:06:23
  • 细品Java8中hashCode方法的使用

    2023-10-04 14:01:19
  • C#多线程系列之线程池

    2023-02-21 17:25:44
  • jsp、struts、spring、mybatis实现前端页面功能模块化拆分的方案

    2023-11-25 07:38:00
  • Java 使用poi把数据库中数据导入Excel的解决方法

    2022-09-19 14:18:52
  • C#请求http向网页发送接收数据的方法

    2022-05-28 15:39:20
  • Java枚举类型enum的详解及使用

    2023-08-02 14:23:57
  • Java实现导出word表格的示例详解

    2023-01-02 21:36:48
  • Unity时间戳的使用方法

    2023-11-11 05:19:00
  • Java生成和解析XML格式文件和字符串的实例代码

    2023-01-25 08:22:44
  • Android 检测键盘显示或隐藏键盘的实现代码

    2022-09-08 09:14:05
  • Java面试重点中的重点之Elasticsearch核心原理

    2021-08-03 07:34:16
  • asp之家 软件编程 m.aspxhome.com