当前位置:网站首页>对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
边栏推荐
- There are some problems when using numeric type to query string type fields in MySQL
- QFileDialog select multiple files or folders
- Find the largest of 3 strings (no more than 20 characters per string).
- Description of the abnormity that the key frame is getting closer and closer in the operation of orb slam
- The annotation is self-defined by implementing the parameter parser handlermethodargumentresolver interface
- JS converts tree structure data into one-dimensional array data
- 5.6 comprehensive case - RTU-
- User manual of Chinese version of solidity ide Remix
- One click cleanup of pycharm and jupyter cache files under the project
- NFT ecological development of Ignis public chain: unicorn Donation and development of Art
猜你喜欢
ansible自動化運維詳解(一)ansible的安裝部署、參數使用、清單管理、配置文件參數及用戶級ansible操作環境構建
The whole house intelligence bet by the giant is driving the "self revolution" of Hisense, Huawei and Xiaomi
AQS & ReentrantLock 实现原理
AAAI 2022招募讲者啦!!
[learning] audio and video development from scratch (9) -- nuplayer
mysql查询字符串类型的字段使用数字类型查询时问题
dried food! Point based: differentiable Poisson solver
clang 如何产生汇编文件
Vowel substring in statistical string of leetcode simple problem
CSV column extract column extraction
随机推荐
干货!以点为形:可微分的泊松求解器
Green apple film and television system source code film and television aggregation film and television navigation film and television on demand website source code
一键清理项目下pycharm和Jupyter缓存文件
数据的删除和修改操作(mysql)
Data deletion and modification (MySQL)
2022.4.11-4.17 AI行业周刊(第93期):AI行业的困局
社区团购小程序源码+界面diy+附近团长+供应商+拼团+菜谱+秒杀+预售+配送+直播
nn.Module类的讲解
How to read books and papers
LeetCode簡單題之計算字符串的數字和
Search the complete navigation program source code
A simple theme of Typecho with beautiful appearance_ Scarfskin source code download
ansible自動化運維詳解(一)ansible的安裝部署、參數使用、清單管理、配置文件參數及用戶級ansible操作環境構建
Install MySQL for Ubuntu and query the average score
js常用数组方法
LeetCode简单题之重新排列日志文件
Comparison of indoor positioning methods of several intelligent robots
情境领导者-第七章、解决绩效问题
Goland 调试go使用-大白记录
通过实现参数解析器HandlerMethodArgumentResolver接口来自定义注解