java.io.writer API 以及 源码解读
声明 我看的是java7的API文档。
如下图所示,java.io.writer 继承了java.lang.Object,实现的接口有Closeable, Flushable, Appendable, AutoCloseable。
所有直接继承它的子类有BufferedWriter CharArrayWriter FilterWriter OutputStreamWriter PipedWriter PrintWriter StringWriter。

Writer是用来操作字符流的抽象类。所有继承它的子类必须要重写的方法有write(char[], int, int), flush(), and close().
下面是java.io.Writer的源码。
package java.io;
public abstract class Writer implements Appendable,Closebale,Flushable{
private char[] writeBuffer;
private static final int WRITE_BUFFER_SIZE = 1024;
projected Object lock;
protected Writer(){
this.lock = this;
}
protected Writer(Object lock){
if(lock == null){
throw new NullPointerException();
}
this.lock = lock;
}
public void write(int c) throw IOException{
syschronized (lock){
if (writeBuffer == null){
writeBuffer = new char[WRITE_BUFFER_SIZE];
}
writeBuffer[0] = (char) c;
write(writeBuffer,0,1);
}
}
public void write(char cbuf[]) throw IOException{
write(cbuf, 0, cbuf.length);
}
abstract public void write(char buf[], int off, int len) throw IOException;
public void write(String str) throw IOException{
write(str, 0, str.length());
}
public void write(String str, int off, int len) throw IOException{
syschronized(lock){
char cbuf[];
if(len <= WRITE_BUFFER_SIZE){
if(writeBuffer == null){
writeBuffer = new char[WRITE_BUFFER_SIZE];
}
cbuf = writeBuffer;
}else{
cbuf = new char[len];
}
str.getChars(off, (off + len), cbuf, 0);
write(cbuf,0,len);
}
}
public Writer append(CharSequence csq) throws IOException{
if(csq == null)
write("null");
else
write(csq.toString());
return this;
}
public Writer append(CharSequence csq, int start, int end) throw IOException{
CharSequence cs = (csq == null ? "null" : csq);
write(cs.subSequence(start,end).toString());
return this;
}
public Writer append(char c) throw IOException{
write(c);
return this;
}
abstract public void flush() throw IOException;
abstract public void close() throw IOException;
}
可以看到在Writer类中子类必须重写的类有三个,
1、abstract public void write(char buf[], int off, int len) throw IOException;
2、abstract public void flush() throw IOException;
3、abstract public void close() throw IOException;
其中,下面三个方法是实现Appendable接口必须实现的方法
1、public Writer append(CharSequence csq) throws IOException
2、public Writer append(CharSequence csq, int start, int end) throw IOException
3、public Writer append(char c) throw IOException
实现 Flushable接口必须实现的方法是
abstract public void flush() throw IOException;
实现Closeable接口必须实现的方法是
abstract public void close() throw IOException;
java.io.writer API 以及 源码解读的更多相关文章
- java.io.BufferedWriter API 以及源码解读
下面是java se 7 API 对于java.io.BufferedWriter 继承关系的描述. BufferedWriter可以将文本写入字符流.它会将字符缓存,目的是提高写入字符的效率. bu ...
- 图解 Java IO : 二、FilenameFilter源码
Writer :BYSocket(泥沙砖瓦浆木匠) 微 博:BYSocket 豆 瓣:BYSocket FaceBook:BYSocket Twitter ...
- 图解 Java IO : 一、File源码
Writer :BYSocket(泥沙砖瓦浆木匠) 微 博:BYSocket 豆 瓣:BYSocket FaceBook:BYSocket Twitter ...
- Java IO 之 FileInputStream & FileOutputStream源码分析
Writer :BYSocket(泥沙砖瓦浆木匠) 微 博:BYSocket 豆 瓣:BYSocket FaceBook:BYSocket Twitter ...
- OutputStreamWriter API 以及源码解读
OutputStreamWriter是字符流与字节流之间的桥梁. 通过它写入的字符流可以通过特殊的字符集转化为字节流.这个特殊的字符集可以指定,也可以采用平台默认的字符集. 每一次调用write()方 ...
- java jdk 中HashMap的源码解读
HashMap是我们在日常写代码时最常用到的一个数据结构,它为我们提供key-value形式的数据存储.同时,它的查询,插入效率都非常高. 在之前的排序算法总结里面里,我大致学习了HashMap的实现 ...
- java.lang.system 类源码解读
通过每块代码进行源码解读,并发现源码使用的技术栈,扩展视野. registerNatives 方法解读 /* register the natives via the static initializ ...
- 【Java集合】ArrayDeque源码解读
简介 双端队列是一种特殊的队列,它的两端都可以进出元素,故而得名双端队列. ArrayDeque是一种以循环数组方式实现的双端队列,它是非线程安全的. 它既可以作为队列也可以作为栈. 继承体系 Arr ...
- Vue 源码解读(5)—— 全局 API
目标 深入理解以下全局 API 的实现原理. Vue.use Vue.mixin Vue.component Vue.filter Vue.directive Vue.extend Vue.set V ...
随机推荐
- Asp.Net Core 依赖注入默认DI,Autofac注入
使用默认DI 修改Startup类方法ConfigureServices如下: public void ConfigureServices(IServiceCollection services) { ...
- npm 的使用指南
npm 使用指南 因为有写关于node.js的配置的博客,还有node和gulp的前端信息配置使用,其中有很多命令都用到了npm.所以这里要着重介绍一下npm. 1 npm介绍 npm(mode pa ...
- 扫描线(线段树)+贪心 ZOJ 3953
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5572 Intervals Time Limit: 1 Second ...
- sklearn中的model_selection模块(1)
sklearn作为Python的强大机器学习包,model_selection模块是其重要的一个模块: 1.model_selection.cross_validation: (1)分数,和交叉验证分 ...
- Liunx 下载文件夹下所有文件
136down voteaccepted You may use this in shell: wget -r --no-parent http://abc.tamu.edu/projects/tzi ...
- Codeforces Round #419 (Div. 2) A-E
上紫啦! E题1:59压哨提交成功翻盘 (1:00就做完了调了一个小时,还好意思说出来? (逃)) 题面太长就不复制了,但是配图很可爱所以要贴过来 九条可怜酱好可爱呀 A - Karen and Mo ...
- 【BZOJ】1036 [ZJOI2008]树的统计Count
[算法]树链剖分+线段树 [题解]模板题,见http://www.cnblogs.com/onioncyc/p/6207462.html 调用线段数时要用新编号pos[i] !!! #include& ...
- [bzoj 2460]线性基+贪心+证明过程
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2460 网上很多题目都没说这个题目的证明,只说了贪心策略,我比较愚钝,在大神眼里的显然的策略 ...
- 【译】Attacking XML with XML External Entity Injection (XXE)
原文链接:Attacking XML with XML External Entity Injection (XXE) XXE:使用XML外部实体注入攻击XML 在XML中,有一种注入外部文件的方式. ...
- localStorage H5本地存储
域内安全.永久保存.即客户端或浏览器中来自同一域名的所有页面都可访问localStorage数据且数据除了删除否则永久保存,但客户端或浏览器之间的数据相互独立. <!doctype html&g ...