当前位置:网站首页>Misunderstanding of flush () method of OutputStream class
Misunderstanding of flush () method of OutputStream class
2022-04-23 08:25:00 【beinlife】
Recently used java Of FileOutputStream Writing documents , Call to flush() Method .
So I looked at it FileInputStream Source code of class , Find out flush() In fact, it inherits from its parent class OutputStream Of .
and OutputStream Class flush() But I didn't do anything , See light suddenly , It's “ Look at the truth of the source code ”.
Actually flush() yes Flushable Interface method , The comment on this method in the official document is “Flushes this output stream and forces any buffered output bytes to be written out.”.
OutputStream Method implemented Flushable Interface , And did nothing , It's really confusing , So there was my misunderstanding .
So when flush() It works ?
The answer is : When OutputStream yes BufferedOutputStream when .
When writing files, you need flush() The effect of , need
FileOutputStream fos = new FileOutputStream("c:\a.txt");
BufferedOutputStream bos = new BufferedOutputStream(fos);
in other words , Need to put FileOutputStream As BufferedOutputStream The arguments of the constructor are passed in , Then on BufferedOutputStream Write operation , To take advantage of the buffer and flush().
see BufferedOutputStream Source code , Find the so-called buffer It's really just a byte[].
BufferedOutputStream Every time write It's actually writing content into byte[], When buffer When the capacity reaches the upper limit , Will trigger a real disk write .
Another way to trigger a disk write is to call flush() 了 .
版权声明
本文为[beinlife]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230734169269.html
边栏推荐
- SYS_CONNECT_BY_PATH(column,'char') 结合 start with ... connect by prior
- 获取TrustedInstaller权限
- 输入/输出系统
- ASAN 极简原理
- ansible自動化運維詳解(一)ansible的安裝部署、參數使用、清單管理、配置文件參數及用戶級ansible操作環境構建
- Community group purchase applet source code + interface DIY + nearby leader + supplier + group collage + recipe + second kill + pre-sale + distribution + live broadcast
- Listed on the Shenzhen Stock Exchange: the market value is 5.2 billion yuan. Lu is the East and his daughter is American
- Comparison of indoor positioning technology
- 记录:js删除数组中某一项或几项的几种方法
- 剑指offer day24 数学(中等)
猜你喜欢
An article understands variable lifting
mysql查询字符串类型的字段使用数字类型查询时问题
Input / output system
CGM optimizes blood glucose monitoring and management -- Yiyu technology appears in Sichuan International Medical Exchange Promotion Association
Failed to convert a NumPy array to a Tensor(Unsupported Object type int)
CGM优化血糖监测管理——移宇科技亮相四川省国际医学交流促进会
vslam PPT
Flink SQL实现流批一体
作文以记之 ~ 二叉树的后序遍历
Rotation function of leetcode medium problem
随机推荐
SYS_CONNECT_BY_PATH(column,'char') 结合 start with ... connect by prior
多目视觉SLAM
Flink SQL实现流批一体
freertos学习02-队列 stream buffer message buffer
[effective go Chinese translation] part I
excle加水印
有意思的js 代码
单点登录 SSO
QT compilation qtxlsx Library
The annotation is self-defined by implementing the parameter parser handlermethodargumentresolver interface
PyQt5开发之QTableWidget表头自定义与美化(附源代码下载)
Somme numérique de la chaîne de calcul pour un problème simple de leetcode
耳穴诊疗随笔0421
[explanation] get ora-12838: cannot read / modify an object after modifying it in parallel
通过实现参数解析器HandlerMethodArgumentResolver接口来自定义注解
redis主从服务器问题
Situational leaders - Chapter 7, solving performance problems
Sword finger offer Day24 math (medium)
Ansible Automation Operation and Maintenance details (ⅰ) Installation and Deployment, Parameter use, list Management, Profile Parameters and user level ansible operating environment Construction
QFileDialog 选择多个文件或文件夹