BufferedWriter中write与close函数使用
BufferedWriter 是一个缓冲字符输出流,可以将要输出的内容先缓冲到一个字符数组中,等字符数组满了才一次性写到输出流内,默认的字符数组长度为8192。使用BufferedWriter 时需要对write与close函数有一定了解,看如下代码:
StringBuffer content = new StringBuffer();
BufferedWriter bWriter = new BufferedWriter(new FileWriter(file, false)); content.setLength(0);
bWriter.write(content.toString());
bWriter.close();
问题:
1. BufferedWriter write函数写入空字符串时会怎么样?
2. BufferedWriter close函数能否关闭FileWriter的文件流 ?
源代码(jdk1.6)解读:
public void write(String paramString) throws IOException {
write(paramString, 0, paramString.length());
}
public void write(String arg0, int arg1, int arg2) throws IOException {
Object arg3 = this.lock;
synchronized (this.lock) {
this.ensureOpen();
int arg4 = arg1;
int arg5 = arg1 + arg2;
while (arg4 < arg5) {
int arg6 = this.min(this.nChars - this.nextChar, arg5 - arg4);
arg0.getChars(arg4, arg4 + arg6, this.cb, this.nextChar);
arg4 += arg6;
this.nextChar += arg6;
if (this.nextChar >= this.nChars) {
this.flushBuffer();
}
}
}
}
答案1:content.setLength(0) 将字符串content 的长度设置为0,content不为null,所以content.toString()为'',一个空字符串。bWriter.write写入null会报错,但是写入''时不会报错,从源代码中可以看到当写入长度为0的字符串时,arg4==arg5,循环不会执行,也不会报错,能够正常处理。
public void close() throws IOException {
Object arg0 = this.lock;
synchronized (this.lock) {
if (this.out != null) {
try {
Writer arg1 = this.out;
Throwable arg2 = null;
try {
this.flushBuffer();
} catch (Throwable arg21) {
arg2 = arg21;
throw arg21;
} finally {
if (arg1 != null) {
if (arg2 != null) {
try {
arg1.close();
} catch (Throwable arg20) {
arg2.addSuppressed(arg20);
}
} else {
arg1.close();
}
}
}
} finally {
this.out = null;
this.cb = null;
}
}
}
}
答案2:当BufferedWriter 关闭时,bWriter.close函数能够关闭FileWriter的文件流。从源代码中可以看出,bWriter.close()在close时会调用关闭FileWriter文件输出流, 其中,this.out 就是FileWriter对象,是被关闭了的。
BufferedWriter中write与close函数使用的更多相关文章
- SQL中Round(),Floor(),Ceiling()函数的浅析
项目中的一个功能模块上用到了标量值函数,函数中又有ceiling()函数的用法,自己找了一些资料,对SQL中这几个函数做一个简单的记录,方便自己学习.有不足之处欢迎拍砖补充 1.round()函数遵循 ...
- avascript中的this与函数讲解
徐某某 一个半路出家的野生程序员 javascript中的this与函数讲解 前言 javascript中没有块级作用域(es6以前),javascript中作用域分为函数作用域和全局作用域.并且,大 ...
- PHP中有关正则表达式的函数集锦
之前学正则表达式的目的是想从网上抓取点小说啊,文档啊,还有获取相应的视频连接然后批量下载.当时初学PHP根本不知道PHP有专门抓包的工具,就像Simple_html_dom.php(在我的其他博文中有 ...
- SQL SERVER中用户定义标量函数(scalar user defined function)的性能问题
用户定义函数(UDF)分类 SQL SERVER中的用户定义函数(User Defined Functions 简称UDF)分为标量函数(Scalar-Valued Function)和表值函数(T ...
- mysql中bit_count和bit_or函数的含义
翻阅mysql手册时,看到有个示例使用了bit_or方法来去除重复的数据,一开始没看明白,后来看明白之后感觉非常巧妙.示例要实现的功能就是计算每月有几天有访问,先把示例摘录在这里. 1 2 3 4 5 ...
- C#中的日期处理函数
C#中的日期处理函数 //2013年4月24日 this.TextBox6.Text = System.DateTime.Now.ToString("D"); //2013-4-2 ...
- 在Excel中使用频率最高的函数的功能和使用方法
在Excel中使用频率最高的函数的功能和使用方法,按字母排序: 1.ABS函数 函数名称:ABS 主要功能:求出相应数字的绝对值. 使用格式:ABS(number) 参数说明:number代表需要求绝 ...
- Loadrunner中web_find和web_reg_find函数的使用与区别
总结一下Loadrunner中的检查点函数,主要介绍两个函数:web_find()和web_reg_find():这两个函数均用于内容的查找,但两者也有本质的区别,具体介绍如下:一.web_find( ...
- python 中的sort 和java中的Collections.sort()函数的使用
x=[1,2,3] x.sort()对的,x这个都变了 y=x.sort()错误 y=sorted(x)对的,x拍好序的一个副本 python中用匿名函数和自定义函数排序:(很奇怪的是比较函数返回的是 ...
随机推荐
- numpy相关使用
相关学习资料 : numpy中文网 https://www.numpy.org.cn/ 1 numpy索引区间为左闭右开,第一个索引能取到,第二个索引取不到 索引内可加步长 如 import nump ...
- 如何用SAP WebIDE的Fiori创建向导基于ABAP OData service快速创建UI5应用
如果我们手上已经有可以正常工作的OData服务,无论位于ABAP on-premise系统还是public上的internet OData service,都可以用SAP WebIDE里的Fiori创 ...
- 【大数据】SmallFile-Analysis-Script
1.root账号先在namenode节点上配置一个定时任务,将fsimage定时传到其他客户机上进行操作 whereis hadoop命令确定安装目录,然后去配置文件找到namenode节点(data ...
- Route Loops
当网络10.4.0.0发生故障时,RouterC检测到故障,并停止其E0接口的路由报文. 然而,路由器A和B还没有收到失败的通知. 路由器A仍然认为可以通过路由器B访问10.4.0.0.路由器A的路由 ...
- BCB6 使用TZCompressionStream压缩
最近由于项目需要涉及到解压第三方公司的数据,在此做一下记录环境部署和使用方法,免得以后忘记. 对方公司的数据是通过TCompressionStream 压缩之后,存到数据库中,采用的 ...
- 怎奈风云多变换,骚完一波还一波,记PHP mongodb驱动的2019年11月用法
怎么,觉得pecl下一个扩展包,phpize make make install php.ini里引用一下 mongodb.so就万事大吉了? Deeply Sorry!看到MongoDB\Driv ...
- Codeforces Round #596 (Div. 2, based on Technocup 2020 Elimination Round 2) B2. TV Subscriptions (Hard Version)
链接: https://codeforces.com/contest/1247/problem/B2 题意: The only difference between easy and hard ver ...
- Educational Codeforces Round 74 (Rated for Div. 2) B. Kill 'Em All
链接: https://codeforces.com/contest/1238/problem/B 题意: Ivan plays an old action game called Heretic. ...
- const int* p
若纠结于const int* p,int const* p,int* const p这三个指针,可以看视频 https://www.icourse163.org/learn/BUPT-10035640 ...
- 【题解】球迷购票问题-C++
题目背景 盛况空前的足球赛即将举行.球赛门票售票处排起了球迷购票长龙. 按售票处规定,每位购票者限购一张门票,且每张票售价为50元.在排成长龙的球迷中有N个人手持面值50元的钱币,另有N个人手持面值1 ...