声明 我看的是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. 树链剖分处理+线段树解决问题 HDU 5029

    http://acm.split.hdu.edu.cn/showproblem.php?pid=5029 题意:n个点的树,m次操作.每次操作输入L,R,V,表示在[L,R]这个区间加上V这个数字.比 ...

  2. (知识扩展)R运用领域一览表

    • Applications and Case Studies - Lessons and Experiences • Big Data Analytics • Biomedical and Heal ...

  3. 【BZOJ】1143: [CTSC2008]祭祀river

    [题意]求DAG上最多的点使得互不可达. [算法]floyd+最大匹配 [题解] 链是DAG上的一个点集,集合内的点相互单向可达. 反链是DAG上的一个点集,集合内的点相互不可达. 题目显然是求最长反 ...

  4. 【转载】iPhone系统概览

    iPhone OS OverviewiPhone系统概览iPhone OS comprises the operating system and technologies that you use t ...

  5. 通过删除hbase表中的region来达到删除表中数据

    公司最近在搞一个hbase删除数据,由于在建表的时候是通过region来对每日的数据进行存储的,所以要求在删除的时候直接通过删除region的来删除数据(最好的方案是只删除region中的数据,不把r ...

  6. Traffic-Server配置(待补充和更新)

    Server 5.3.2 测试1.裸盘:remap.configmap http://192.168.227.131 http://192.168.227.131:8080 #traffic_serv ...

  7. iOS中响应者链条-触摸事件,hitTest方法坐标转换

    总体来说,分2个步骤: 一,从上到下寻找合适的控件来处理这个触摸事件.如下图,如果点击了黄色4,则UIApplication -> UIWindow -> 1白色 -> 2橙色 -& ...

  8. python3.4.3安装allure2记录

    一.安装:cmd执行命令pip install allure-pytest 二.下载allure2:2.7.0版本 https://dl.bintray.com/qameta/generic/io/q ...

  9. ubuntu遇到的 the system is runing low-graphics mode 问题

    不知道修改了什么,然后开机显示the system is runing low-graphics mode 看过博客使用如下方法成功进入系统,但是显示分辨率很低,显示 built-in display ...

  10. 批量删除.svn文件夹和.svn文件

    新建可运行文件 Windows环境 将下面的代码保存为 kill-svn.bat文件,放到要删除.svn文件的目录下,双击运行即可 @echo on @rem 删除SVN版本控制目录 @rem for ...