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中用匿名函数和自定义函数排序:(很奇怪的是比较函数返回的是 ...
随机推荐
- iOS音频频谱动画,仿QQ录音频谱
先上效果图: display.gif 有需要的请移步GitHub下载: https://github.com/HuangGY1993/GYSpectrum 用法很简单,示例: SpectrumView ...
- /Android/sdk/build-tools/21.1.2/aapt'' finished with non-zero exit value 42
相信很多朋友都会遇到 Error:Error: com.android.ide.common.process.ProcessException: org.gradle.process.internal ...
- JdbcTemplate批量插入数据
运行环境:SpringBoot,注入JdbcTemplate @Autowired private JdbcTemplate jdbcTemplate; 1.单表批量插入数据 @Test public ...
- Window10下Python3.7 安装与卸载
1.进入官网https://www.python.org/,点击Downloads下的Windows按钮,进入下载页面. 2.如下图所示,点击下载. 3.安装Python3.7.4 4.打开cmd,输 ...
- 异步网络编程aiohttp的使用
aiohttp的使用 aiohttp Asynchronous HTTP Client/Server for asyncio and Python. Supports both Client and ...
- Windows安全应急响应(一)
入侵排查思路 检查账号安全 1.查看服务器是否有弱口令,远程管理端口是否对公网开放 2.查看服务器是否存在可以账号.新增账号 3.查看服务器是否存在隐藏账号.克隆账号检查方法:i.打开注册表,查看管理 ...
- Python 常用的ORM框架简介
ORM概念ORM(Object Ralational Mapping,对象关系映射)用来把对象模型表示的对象映射到基于S Q L 的关系模型数据库结构中去.这样,我们在具体的操作实体对象的时候,就不需 ...
- 云计算下的企业IT运维
云计算管理员们一般都工作在一个分布式局域网计算基础设施中,它与传统数据中心最大的区别之一就是,所有被存储.调配和管理的数据都在一个私有云中.基于云计算的高效工作负载监控可在性能发生问题之前就提前发现这 ...
- IdentityServer(三)密码模式
前言 用户名密码模式相较于客户端凭证模式,多了用户.通过用户的用户名和密码向Identity Server申请访问令牌.密码模式有两种实现方式. 1.把用户写进内存Identity从中读取账号密码验证 ...
- mysql 5.5 编码设置为utf8 转载自:http://outofcontrol.ca/thoughts/comments/change-mysql-5.5-default-character-set-to-utf8
Change MySQL 5.5 default character-set to UTF8 连接里是linux下的 在window下my.ini Add under [client] the fo ...