当前位置:网站首页>对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
边栏推荐
- Why are there 1px problems? How?
- QFileDialog 选择多个文件或文件夹
- The annotation is self-defined by implementing the parameter parser handlermethodargumentresolver interface
- 获取TrustedInstaller权限
- Search the complete navigation program source code
- 将实例化对象的方法 给新的对象用
- Data security has become a hidden danger. Let's see how vivo can make "user data" armor again
- LeetCoed18. Sum of four numbers
- vslam PPT
- Rearranging log files for leetcode simple question
猜你喜欢

Qt读写XML文件

nn.Module类的讲解

Samsung, March to the west again

Comparison of indoor positioning methods of several intelligent robots
![[untitled]](/img/bb/213d95b60651dfeadb239a70507506.png)
[untitled]

Kubernetes in browser and IDE | interactive learning platform killercoda

Search the complete navigation program source code

LeetCode简单题之重新排列日志文件

Ansible Automation Operation and Maintenance details (ⅰ) Installation and Deployment, Parameter use, list Management, Profile Parameters and user level ansible operating environment Construction

Rearranging log files for leetcode simple question
随机推荐
Find the largest of 3 strings (no more than 20 characters per string).
常用正则表达式
获取TrustedInstaller权限
LeetCode简单题之重新排列日志文件
Rearranging log files for leetcode simple question
跨域配置报错: When allowCredentials is true, allowedOrigins cannot contain the special value “*“
Why are there 1px problems? How?
网赚APP资源下载类网站源码
Data security has become a hidden danger. Let's see how vivo can make "user data" armor again
Search the complete navigation program source code
[appium] encountered the problem of switching the H5 page embedded in the mobile phone during the test
2022.4.11-4.17 AI industry weekly (issue 93): the dilemma of AI industry
Sword finger offer Day24 math (medium)
Briefly describe the hierarchical strategy of memory
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
如何保护开源项目免遭供应链攻击-安全设计(1)
LeetCode简单题之三除数
js常用数组方法
Qtablewidget header customization and beautification developed by pyqt5 (with source code download)
QT reading and writing XML files