当前位置:网站首页>对OutputStream类的flush()方法的误解
对OutputStream类的flush()方法的误解
2022-04-23 07:34:00 【beinlife】
最近使用java的FileOutputStream写文件,调用到了flush()方法。
于是查看了FileInputStream类的源代码,发现flush()其实是继承于其父类OutputStream的。
而OutputStream类的flush()却什么也没做,恍然大悟,真是“看源代码者得真相啊”。
其实flush()是Flushable接口的方法,官方文档的对该方法的注释是“Flushes this output stream and forces any buffered output bytes to be written out.”。
OutputStream方法实现了Flushable接口,而又什么也没做,真是让人一头雾水,于是就出现了我的误解。
那么什么时候flush()才有效呢?
答案是:当OutputStream是BufferedOutputStream时。
当写文件需要flush()的效果时,需要
FileOutputStream fos = new FileOutputStream("c:\a.txt");
BufferedOutputStream bos = new BufferedOutputStream(fos);
也就是说,需要将FileOutputStream作为BufferedOutputStream构造函数的参数传入,然后对BufferedOutputStream进行写入操作,才能利用缓冲及flush()。
查看BufferedOutputStream的源代码,发现所谓的buffer其实就是一个byte[]。
BufferedOutputStream的每一次write其实是将内容写入byte[],当buffer容量到达上限时,会触发真正的磁盘写入。
而另一种触发磁盘写入的办法就是调用flush()了。
版权声明
本文为[beinlife]所创,转载请带上原文链接,感谢
https://blog.csdn.net/beinlife/article/details/52678567
边栏推荐
- 【路科V0】验证环境2——验证环境组件
- 监控智能回放是什么,如何使用智能回放查询录像
- Green apple film and television system source code film and television aggregation film and television navigation film and television on demand website source code
- PHP high precision computing
- Vowel substring in statistical string of leetcode simple problem
- WordPress love navigation theme 1.1.3 simple atmosphere website navigation source code website navigation source code
- Brief description of CPU
- 分组背包呀
- dmp引擎工作总结(2021,光剑)
- Jetson Xavier NX (3) bazel mediapipe installation
猜你喜欢

LeetCode簡單題之計算字符串的數字和

一键清理项目下pycharm和Jupyter缓存文件

怎么读书读论文

为什么会存在1px问题?怎么解决?

Comparison of indoor positioning methods of several intelligent robots

Detailed explanation of ansible automatic operation and maintenance (I) installation and deployment, parameter use, list management, configuration file parameters and user level ansible operating envi

Listed on the Shenzhen Stock Exchange: the market value is 5.2 billion yuan. Lu is the East and his daughter is American

Campus transfer second-hand market source code download

浅谈ES6尾调优化

AAAI 2022 recruit speakers!!
随机推荐
編譯原理題-帶答案
396. Rotate Function
Rearranging log files for leetcode simple question
分布式消息中间件框架选型-数字化架构设计(7)
社区团购小程序源码+界面diy+附近团长+供应商+拼团+菜谱+秒杀+预售+配送+直播
获取TrustedInstaller权限
Green apple film and television system source code film and television aggregation film and television navigation film and television on demand website source code
My heart's broken! A woman's circle of friends envied others for paying wages on time and was fired. Even her colleagues who liked her were fired together
Qt编译QtXlsx库
Install MySQL for Ubuntu and query the average score
Listed on the Shenzhen Stock Exchange: the market value is 5.2 billion yuan. Lu is the East and his daughter is American
利用Js实现一个千分位
An article understands variable lifting
数据的删除和修改操作(mysql)
Talk about the basic but not simple stock data
干货!以点为形:可微分的泊松求解器
AAAI 2022招募讲者啦!!
校园转转二手市场源码下载
【深度好文】Flink SQL流批⼀体化技术详解(一)
Situational leaders - Chapter 7, solving performance problems