惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Simon Willison's Weblog
Simon Willison's Weblog
G
Google Developers Blog
Spread Privacy
Spread Privacy
I
InfoQ
V
V2EX
S
Schneier on Security
小众软件
小众软件
C
CERT Recently Published Vulnerability Notes
博客园 - 聂微东
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Stack Overflow Blog
Stack Overflow Blog
T
Threat Research - Cisco Blogs
L
Lohrmann on Cybersecurity
Recent Announcements
Recent Announcements
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Attack and Defense Labs
Attack and Defense Labs
云风的 BLOG
云风的 BLOG
The Hacker News
The Hacker News
S
SegmentFault 最新的问题
C
Cybersecurity and Infrastructure Security Agency CISA
NISL@THU
NISL@THU
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
GbyAI
GbyAI
Latest news
Latest news
S
Secure Thoughts
Project Zero
Project Zero
MongoDB | Blog
MongoDB | Blog
I
Intezer
Security Latest
Security Latest
Apple Machine Learning Research
Apple Machine Learning Research
Vercel News
Vercel News
N
Netflix TechBlog - Medium
V2EX - 技术
V2EX - 技术
量子位
T
Threatpost
T
The Blog of Author Tim Ferriss
Y
Y Combinator Blog
T
Tor Project blog
A
Arctic Wolf
Microsoft Security Blog
Microsoft Security Blog
T
The Exploit Database - CXSecurity.com
大猫的无限游戏
大猫的无限游戏
T
Tailwind CSS Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
C
Check Point Blog
博客园 - Franky
Google DeepMind News
Google DeepMind News
The Register - Security
The Register - Security
The GitHub Blog
The GitHub Blog
L
LINUX DO - 热门话题

博客园 - 港城大白鲨

SQLServer2022安装记录 Go 语言从入门到理解 Oracle日常问题集锦 java.lang.ClassNotFoundException: javax.servlet.http.HttpServlet 源码、反码、补码详解及Byte结构 SSL证书类型OV、EV、DV说明来源阿里云 Servlet导出Excel,Json content-type的理解和日常实践 .NET版本发布历史 C# 部分类 详解C#中 Thread,Task,Async/Await,IAsyncResult【转】 企业微信-自建应用使用审批流程引擎 C# 操作Excel 常用整理 SQLAlwayson搭建过程之三 AlwaysOn Failover Cluster Instance SQLAlwayson搭建过程之二 AlwaysOn可用性组 SQLAlwayson搭建过程之一故障转移集群WSFC Sharepoint server2019 通讯组(安全组)发送邮件问题 SQLServer链接服务器 C# 操作Exchange 的powershell以实现邮件撤回
IO流操作flush应用场景
港城大白鲨 · 2024-02-22 · via 博客园 - 港城大白鲨

一、刷新(flush)与关闭(close)

在进行IO流操作的时候,经常会遇到flush()和close()方法,有时候会搞不清什么时候需要flush,索性就所有时候都flush

  • flush:刷新缓冲区,流对象可以继续使用。
  • close: 先刷新缓冲区,然后通知系统释放资源。流对象不可以再被使用了。
@Test
public void t3() throws IOException {
	FileWriter fileWriter = new FileWriter("D:\\temp\\temp2.txt",true);
	fileWriter.write("你好,你是一个好人");
	fileWriter.flush();//正确示范
	fileWriter.write("hello world");
	fileWriter.close();
}
@Test
public void t4() throws IOException {
	FileOutputStream fileOutputStream = new FileOutputStream("D:\\temp\\temp2.txt");
	fileOutputStream.write("你好,你是一个好人".getBytes());
	fileOutputStream.flush();//错误示范
	fileOutputStream.write("hello world".getBytes());
	fileOutputStream.close();
}
@Test
public void t5() throws IOException {
	FileOutputStream fileOutputStream = new FileOutputStream("D:\\temp\\temp2.txt");
	BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
	bufferedOutputStream.write("你好,你是一个好人".getBytes());
	fileOutputStream.flush();//错误示范
	bufferedOutputStream.write("hello world".getBytes());
	fileOutputStream.close();
}

在close之前调用flush总没有错,只是会显得不规范,对代码理解不深

1. FileOutputStream 的flush方法是调用父类的一个空方法,自身并没有重写,所以调用这个方法是没有意义的

2. BufferedOutputStream里的flush是有意义的,但close方法里已经调用了这个flush,所以如果只是在关闭的时候可以不用刻意flush

那flush有哪些应用场景呢,比如代码1部分的 如果我想写一部份数据后就写入到文件系统可以手动调用flush(),之前的数据就会写入到文件,然后继续写。