通过源码学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 从 ...
随机推荐
- 加密解密(9)Diffie-Hellman密钥交换协议
过程如下 : 1,小李把KeyX经过加密变化,生成MsgA传给老王. 2,老王得到MsgA,保存在本地. 3,老王把KeyY经过加密变化,生成MsgB传给小李, 4,小李得到MsgB保存在本地, 5, ...
- SecureCRT访问开发板linux系统
前言: 最近在用OK6410开发板跑linux系统,经常在终端上敲一些指令,无奈开发板屏幕太小用起来非常不方便,所以使用终端一款能运行在windows上的软件与开发板连接,直接在电脑上操作开发板了,这 ...
- c# webbrowser 随机点击链接
HtmlElementCollection hec = webBrowser1.Document.All; ; i < hec.Count; i++) { if (hec[i].GetAttri ...
- mac 下 apache设置
windows下面的apache配置 apache是mac下是默认就有的,我们只需使用命令开启.暂停和重启就好了 sudo apachectl start sudo apachectl stop su ...
- km算法的个人理解
首先相对于上个blog讲的匈牙利算法用于解决无权二分图的最佳匹配,km算法则是在匈牙利算法基础上更进一层的,每条边增加了权值后,真的开始看时有些无厘头,觉得没有什么好方法,但两位牛人Kuhn-Munk ...
- BZOJ_1629_[Usaco2007_Demo]_Cow_Acrobats_(贪心)
描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1629 \(n\)头牛叠罗汉.第\(i\)头牛的力量为\(s_i\),重量为\(w_i\),危险 ...
- BZOJ2073: [POI2004]PRZ
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2073 题解:跟风Xs酱! 数据范围这么小,肯定是状压DP.咦?怎么枚举子集?... 跪烂Xs: ...
- websocket webworker
对我来说最快的学习途径是实践,所以找两个东西来练手.一个是websocket一个是webwoker,今天先说第一个. 要理解socket就要先理解http和tcp的区别,简单说就是一个是短链,一个是长 ...
- zoj 2027 Travelling Fee
// 题意 : 一个人要去旅行 给你起点和终点 求最少花费 其中花费为经过路径的总费用减去该路径的中的最大花费段// 直接搜索 稍微加了个剪枝 主要是数据规模小#include <iostrea ...
- 【转】pdf 中如何把几页缩小成一页打印
我用的是Foxit PDF Reader,可以这样设置:文件-打印-打印处理下的页面排列选择“在每张纸上放置多页”-选择每页版数即可. 如果你用的是Adobe Reader,也可以自己找一下,看是否有 ...