声明 我看的是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 以及 源码解读的更多相关文章

  1. java.io.BufferedWriter API 以及源码解读

    下面是java se 7 API 对于java.io.BufferedWriter 继承关系的描述. BufferedWriter可以将文本写入字符流.它会将字符缓存,目的是提高写入字符的效率. bu ...

  2. 图解 Java IO : 二、FilenameFilter源码

    Writer      :BYSocket(泥沙砖瓦浆木匠) 微         博:BYSocket 豆         瓣:BYSocket FaceBook:BYSocket Twitter   ...

  3. 图解 Java IO : 一、File源码

    Writer      :BYSocket(泥沙砖瓦浆木匠) 微         博:BYSocket 豆         瓣:BYSocket FaceBook:BYSocket Twitter   ...

  4. Java IO 之 FileInputStream & FileOutputStream源码分析

    Writer      :BYSocket(泥沙砖瓦浆木匠) 微         博:BYSocket 豆         瓣:BYSocket FaceBook:BYSocket Twitter   ...

  5. OutputStreamWriter API 以及源码解读

    OutputStreamWriter是字符流与字节流之间的桥梁. 通过它写入的字符流可以通过特殊的字符集转化为字节流.这个特殊的字符集可以指定,也可以采用平台默认的字符集. 每一次调用write()方 ...

  6. java jdk 中HashMap的源码解读

    HashMap是我们在日常写代码时最常用到的一个数据结构,它为我们提供key-value形式的数据存储.同时,它的查询,插入效率都非常高. 在之前的排序算法总结里面里,我大致学习了HashMap的实现 ...

  7. java.lang.system 类源码解读

    通过每块代码进行源码解读,并发现源码使用的技术栈,扩展视野. registerNatives 方法解读 /* register the natives via the static initializ ...

  8. 【Java集合】ArrayDeque源码解读

    简介 双端队列是一种特殊的队列,它的两端都可以进出元素,故而得名双端队列. ArrayDeque是一种以循环数组方式实现的双端队列,它是非线程安全的. 它既可以作为队列也可以作为栈. 继承体系 Arr ...

  9. Vue 源码解读(5)—— 全局 API

    目标 深入理解以下全局 API 的实现原理. Vue.use Vue.mixin Vue.component Vue.filter Vue.directive Vue.extend Vue.set V ...

随机推荐

  1. 目前最快速的多线程Kmeans算法,java实现

    目前最快速Kmeans算法,并由java实现!面对很大的K值表现依然很好. 代码地址: https://github.com/Jethu1/fastKmeans #1.这是一个由java实现的的,多线 ...

  2. 数学:Nim游戏和SG函数

    有若干堆石子,两人轮流从中取石子,取走最后一个石子的人为胜利者 以下的性质是显然的 .无法移动的状态是必败态 .可以移动到必败态的局面一定是非必败态 .在必败态做所有操作的结果都是非必败态 在普通Ni ...

  3. PHP与Ajax

    如何用PHP接收JSON格式数据 1.一般来说,我们直接用$_POST $_REQUEST $_GET这样的超全局变量接收就好了 <?php $obj_temp=$_POST['data']; ...

  4. 铺地砖|状压DP练习

    有一个N*M(N<=5,M<=1000)的棋盘,现在有1*2及2*1的小木块无数个,要盖满整个棋盘,有多少种方式?答案只需要mod1,000,000,007即可. //我也不知道这道题的来 ...

  5. 【BZOJ】3771: Triple FTT+生成函数

    [题意]给定n个物品,价值为$a_i$,物品价格互不相同,求选一个或两个或三个的价值为x的方案数,输出所有存在的x和对应方案数.$ai<=40000$. [算法]生成函数+FFT [题解]要求价 ...

  6. Docker 配置国内镜像拉取中心,Configure docker to use faster registries in China.

    Networking in China is really bad when it comes to using some cloud based tools like docker, it's us ...

  7. 在Unity中实现屏幕空间反射Screen Space Reflection(1)

    本篇文章我会介绍一下我自己在Unity中实现的SSR效果 出发点是理解SSR效果的原理,因此最终效果不是非常完美的(代码都是够用就行),但是从学习的角度来说足以学习到SSR中的核心算法. 如果对核心算 ...

  8. php简单文件管理器——php经典实例

    <html> <head> <title>文件管理</title> <meta charset='utf-8' /> </head&g ...

  9. php菜刀分析学习

    这里以eval为例 我们知道, php中的eval能把字符串当代码执行: eval('phpcode'); 注意, 这里的代码要有分号结尾, 我们测试: 我们创建一个最简单的SHELL: <?p ...

  10. 【Windows使用笔记】神舟笔记本的control center

    首先,神船大法好. 然后,因为我的船风扇声音有点大啊,在实验室感觉就很吵,但是它的背板温度又不是很高,所以想设置下风扇的启动. 所以需要用到神船自带的control center软件. 长这样. 应该 ...