通过源码学Java基础:BufferedReader和BufferedWriter
准备写一系列Java基础文章,先拿Java.io下手,今天聊一聊BufferedReader和BufferedWriter
BufferedReader
BufferedReader继承Writer,本身的方法非常简单,其官方解释如下:
Reads text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines.
The buffer size may be specified, or the default size may be used. The default is large enough for most purposes.
简单翻译一下:
从流里面读取文本,通过缓存的方式提高效率,读取的内容包括字符、数组和行。
缓存的大小可以指定,也可以用默认的大小。大部分情况下,默认大小就够了。
1. 构造函数
BufferedReader有两个构造函数,其声明如下:
BufferedReader(Reader in)
Creates a buffering character-input stream that uses a default-sized input buffer.
BufferedReader(Reader in, int sz)
Creates a buffering character-input stream that uses an input buffer of the specified size.
一个是传一个Reader,另外一个增加了缓存的大小。
其真正的实现也很简单,反编译Java源码如下:
public BufferedWriter(Writer paramWriter)
{
this(paramWriter, defaultCharBufferSize);
}
public BufferedWriter(Writer paramWriter, int paramInt)
{
super(paramWriter);
if (paramInt <= 0) {
throw new IllegalArgumentException("Buffer size <= 0");
}
this.out = paramWriter;
this.cb = new char[paramInt];
this.nChars = paramInt;
this.nextChar = 0;
this.lineSeparator = ((String)AccessController.doPrivileged(new GetPropertyAction("line.separator")));
}
常见的初始化方法
BufferedReader br = new BufferedReader(new FileReader("d:/123.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
第一个方法是读取一个文件;第二个方法是从标准输入读。
2. 主要方法
void close()
Closes the stream and releases any system resources associated with it.
void mark(int readAheadLimit)
Marks the present position in the stream.
boolean markSupported()
Tells whether this stream supports the mark() operation, which it does.
int read()
Reads a single character.
int read(char[] cbuf, int off, int len)
Reads characters into a portion of an array.
String readLine()
Reads a line of text.
boolean ready()
Tells whether this stream is ready to be read.
void reset()
Resets the stream to the most recent mark.
long skip(long n)
Skips characters.
提供了三种读数据的方法read、read(char[] cbuf, int off, int len)、readLine(),其中常用的是readLine。
a. read方法
Reads a single character.
这个方法从reader里面读一个字符,并向下移一位。如果读完了,会返回-1.
public void readTest() {
try {
BufferedReader br = new BufferedReader(new FileReader("d:/123.txt"));
for (;;) {
int i = br.read();
if (i == -1) {
break;
}
System.out.print((char) i);
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
b. read(char[] cbuf, int off, int len)方法
public void readCharTest() {
try {
BufferedReader br = new BufferedReader(new FileReader("d:/123.txt"));
char[] buf = new char[100];
br.read(buf);
System.out.print(String.valueOf(buf));
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
如上,先定义一个数组,然后读进数组就可以了。如果数组比内容少,则数组满了就不读了。当然,也可以指定数据的off和len,但一般不用。
c. readline方法
public void readline() {
try {
BufferedReader br = new BufferedReader(new FileReader("d:/123.txt"));
while (true) {
String str = br.readLine();
if (str == null) {
break;
}
System.out.println(str);
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
用null判断是否读完。
d. close方法
Closes the stream and releases any system resources associated with it. Once the stream has been closed, further read(), ready(), mark(), reset(), or skip() invocations will throw an IOException. Closing a previously closed stream has no effect.
用完流之后,一定要close掉。当然,放在finally里面更保险。
3. 其他
BufferedReader的子类包括FileReader, InputStreamReader。因此,这两个子类也有如上方法。
BufferedWriter
BufferedWriter继承自java.io.Writer。
Writes text to a character-output stream, buffering characters so as to provide for the efficient writing of single characters, arrays, and strings.
The buffer size may be specified, or the default size may be accepted. The default is large enough for most purposes.
1. 构造函数
BufferedWriter(Writer out)
Creates a buffered character-output stream that uses a default-sized output buffer.
BufferedWriter(Writer out, int sz)
Creates a new buffered character-output stream that uses an output buffer of the given size.
其源码如下:
public BufferedWriter(Writer paramWriter)
{
this(paramWriter, defaultCharBufferSize);
}
public BufferedWriter(Writer paramWriter, int paramInt)
{
super(paramWriter);
if (paramInt <= 0) {
throw new IllegalArgumentException("Buffer size <= 0");
}
this.out = paramWriter;
this.cb = new char[paramInt];
this.nChars = paramInt;
this.nextChar = 0;
this.lineSeparator = ((String)AccessController.doPrivileged(new GetPropertyAction("line.separator")));
}
用法
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
BufferedWriter bw = new BufferedWriter(new FileWriter("d:/1234.txt", true));
输出到标准输出或者输出到一个文件。
2. 主要方法
void close()
Closes the stream, flushing it first.
void flush()
Flushes the stream.
void newLine()
Writes a line separator.
void write(char[] cbuf, int off, int len)
Writes a portion of an array of characters.
void write(int c)
Writes a single character.
void write(String s, int off, int len)
Writes a portion of a String.
既然是writer,那主要是写数据。
a. write方法
private void writeTest() {
try {
BufferedWriter bw = new BufferedWriter(
new FileWriter("d:/1234.txt"));
bw.write("writeTest");
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
注意,BufferedWriter没有write(String s)或者write(char[] s)的定义,为什么还能直接bw.write("writeTest")呢。因为BufferedWriter的父类Writer实现了这个方法。不过,writer实际上也是调用了BufferedWriter的方法实现的。
public void write(String paramString, int paramInt1, int paramInt2)
throws IOException
{
synchronized (this.lock)
{
char[] arrayOfChar;
if (paramInt2 <= 1024)
{
if (this.writeBuffer == null) {
this.writeBuffer = new char['?'];
}
arrayOfChar = this.writeBuffer;
}
else
{
arrayOfChar = new char[paramInt2];
}
paramString.getChars(paramInt1, paramInt1 + paramInt2, arrayOfChar, 0);
write(arrayOfChar, 0, paramInt2);
}
}
其中write(arrayOfChar, 0, paramInt2)在Writer中是抽象方法,定义如下:
public abstract void write(char[] paramArrayOfChar, int paramInt1, int paramInt2)
throws IOException;
这个是标准的模板模式啊。
b. newLine方法
newLine方法实现了换行,其源码如下:
public void newLine()
throws IOException
{
write(this.lineSeparator);
}
代码很简单,只是写了一个换行符,那么换行符是什么呢?
this.lineSeparator = ((String)AccessController.doPrivileged(new GetPropertyAction("line.separator")));
不过,也有人说换行符有的时候不靠谱,不过我没遇到过。
在Java7之后,也可以这么写,当然,实际上是一样的。
System.lineSeparator()
c. flush方法
flush方法的说明是这样的
Flushes the stream.
也就是说会把内容flush进去,冲进去。
那什么时候用这个呢?答案就是,基本上不用。
先看看flush干了什么事情。
public void flush()
throws IOException
{
synchronized (this.lock)
{
flushBuffer();
this.out.flush();
}
}
就是调用了flushBuffer()方法。
那么close的时候干了什么事情呢?
public void close()
throws IOException
{
synchronized (this.lock)
{
if (this.out == null) {
return;
}
try
{
flushBuffer();
}
finally
{
this.out.close();
this.out = null;
this.cb = null;
}
}
}
注意,也调用了flushBuffer()。也就是说,只要你close了,就不用flush了。
而且,write也会flush,其代码如下:
public void write(int paramInt)
throws IOException
{
synchronized (this.lock)
{
ensureOpen();
if (this.nextChar >= this.nChars) {
flushBuffer();
}
this.cb[(this.nextChar++)] = ((char)paramInt);
}
}
那close和write都flush了,flush还有啥用呢?有人这么说:
In other words, relax - just write, write, write and close
通过源码学Java基础:BufferedReader和BufferedWriter的更多相关文章
- 通过源码学Java基础:InputStream、OutputStream、FileInputStream和FileOutputStream
1. InputStream 1.1 说明 InputStream是一个抽象类,具体来讲: This abstract class is the superclass of all classes r ...
- 通过源码分析Java开源任务调度框架Quartz的主要流程
通过源码分析Java开源任务调度框架Quartz的主要流程 从使用效果.调用链路跟踪.E-R图.循环调度逻辑几个方面分析Quartz. github项目地址: https://github.com/t ...
- 通过源码浅析Java中的资源加载
前提 最近在做一个基础组件项目刚好需要用到JDK中的资源加载,这里说到的资源包括类文件和其他静态资源,刚好需要重新补充一下类加载器和资源加载的相关知识,整理成一篇文章. 理解类的工作原理 这一节主要分 ...
- 通过源码了解Java的自动装箱拆箱
什么叫装箱 & 拆箱? 将int基本类型转换为Integer包装类型的过程叫做装箱,反之叫拆箱. 首先看一段代码 public static void main(String[] args) ...
- 通过源码安装PostgresSQL
通过源码安装PostgresSQL 1.1 下载源码包环境: Centos6.8 64位 yum -y install bison flex readline-devel zlib-devel yum ...
- 通过源码了解ASP.NET MVC 几种Filter的执行过程
一.前言 之前也阅读过MVC的源码,并了解过各个模块的运行原理和执行过程,但都没有形成文章(所以也忘得特别快),总感觉分析源码是大神的工作,而且很多人觉得平时根本不需要知道这些,会用就行了.其实阅读源 ...
- Linux下通过源码编译安装程序
本文简单的记录了下,在linux下如何通过源码安装程序,以及相关的知识.(大神勿喷^_^) 一.程序的组成部分 Linux下程序大都是由以下几部分组成: 二进制文件:也就是可以运行的程序文件 库文件: ...
- 通过源码了解ASP.NET MVC 几种Filter的执行过程 在Winform中菜单动态添加“最近使用文件”
通过源码了解ASP.NET MVC 几种Filter的执行过程 一.前言 之前也阅读过MVC的源码,并了解过各个模块的运行原理和执行过程,但都没有形成文章(所以也忘得特别快),总感觉分析源码是大神 ...
- 在centos6.7通过源码安装python3.6.7报错“zipimport.ZipImportError: can't decompress data; zlib not available”
在centos6.7通过源码安装python3.6.7报错: zipimport.ZipImportError: can't decompress data; zlib not available 从 ...
随机推荐
- MyBatis学习总结(5)——实现关联表查询
一对一关联 提出需求 根据班级id查询班级信息(带老师的信息) 创建表和数据 创建一张教师表和班级表,假设一个老师负责教一个班,那么老师和班级之间的关系就是一对一的关系. create table t ...
- Codeforces Round #207 (Div. 1)B(数学)
数学so奇妙.. 这题肯定会有一个循环节 就是最小公倍数 对于公倍数内的相同的数的判断 就要借助最大公约数了 想想可以想明白 #include <iostream> #include< ...
- jenkins mac slave 设置
1.在jenkins上增加节点, 2,在mac系统中将ssh的服务打开在偏好设置- 互联网与无线 - 共享中 3,使用mac root用户修改sshd-config的鉴权方式 首先获取到root用户登 ...
- parseInt和valueOf
.parseInt和valueOf.split static int parseInt(String s) 将字符串参数作为有符号的十进制整数进行分析. static Integer valueOf( ...
- 可视化PK纯代码
简述 其实今天说的内容不仅仅局限于Qt,在很多其它语言或者框架中也适用,那就是 - 用可视化工具or文本编辑器?拖or不拖? 如果有人问我喜欢脱or不脱?我会毫不犹豫地说不脱,因为我比较矜持O(∩_∩ ...
- delphi 编译的时候 把Warning去除的方法
delphi 编译的时候 把Warning去除的方法 在 添加 {$WARNINGS OFF}
- 面试题 IQ
现在有一大块金条,它可以分为七小块金条.是这样子的,工人为你工作7天,每天都将获得一小块金条,你要做的就是发工资,切割大块金条的次数最多两次,你有什么方法让工人每天都获得一小块金条呢?
- ruby函数回调的实现方法
以前一直困惑ruby不像python,c可以将函数随意传递,然后在需要的时候才去执行.其实本质原因是ruby的函数不是对象. 通过查阅资料发现可以使用如下方法: def func(a, b) puts ...
- iwpriv工具通过ioctl动态获取相应无线网卡驱动的private_args所有扩展参数
iwpriv工具通过ioctl动态获取相应无线网卡驱动的private_args所有扩展参数 iwpriv是处理下面的wlan_private_args的所有扩展命令,iwpriv的实现上,是这样的, ...
- fastdb中的位图应用
位图内存管理: 每块内存用一个二进制位表示它的使用状态,如果该块内存被占用,则把对应位图中的对应位置1,如果空闲则置0,原理十分简单.计算机里面处理的位数最少的变量是字节(byte),所以也就是8位做 ...