声明 我看的是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的源码。

  1. package java.io;
  2.  
  3. public abstract class Writer implements Appendable,Closebale,Flushable{
  4.  
  5. private char[] writeBuffer;
  6. private static final int WRITE_BUFFER_SIZE = 1024;
  7. projected Object lock;
  8.  
  9. protected Writer(){
  10. this.lock = this;
  11. }
  12.  
  13. protected Writer(Object lock){
  14. if(lock == null){
  15. throw new NullPointerException();
  16. }
  17. this.lock = lock;
  18. }
  19.  
  20. public void write(int c) throw IOException{
  21. syschronized (lock){
  22. if (writeBuffer == null){
  23. writeBuffer = new char[WRITE_BUFFER_SIZE];
  24. }
  25. writeBuffer[0] = (char) c;
  26. write(writeBuffer,0,1);
  27. }
  28. }
  29.  
  30. public void write(char cbuf[]) throw IOException{
  31. write(cbuf, 0, cbuf.length);
  32. }
  33.  
  34. abstract public void write(char buf[], int off, int len) throw IOException;
  35.  
  36. public void write(String str) throw IOException{
  37. write(str, 0, str.length());
  38. }
  39.  
  40. public void write(String str, int off, int len) throw IOException{
  41. syschronized(lock){
  42. char cbuf[];
  43. if(len <= WRITE_BUFFER_SIZE){
  44. if(writeBuffer == null){
  45. writeBuffer = new char[WRITE_BUFFER_SIZE];
  46. }
  47. cbuf = writeBuffer;
  48. }else{
  49. cbuf = new char[len];
  50. }
  51. str.getChars(off, (off + len), cbuf, 0);
  52. write(cbuf,0,len);
  53. }
  54. }
  55.  
  56. public Writer append(CharSequence csq) throws IOException{
  57. if(csq == null)
  58. write("null");
  59. else
  60. write(csq.toString());
  61. return this;
  62. }
  63.  
  64. public Writer append(CharSequence csq, int start, int end) throw IOException{
  65. CharSequence cs = (csq == null ? "null" : csq);
  66. write(cs.subSequence(start,end).toString());
  67. return this;
  68. }
  69.  
  70. public Writer append(char c) throw IOException{
  71. write(c);
  72. return this;
  73. }
  74.  
  75. abstract public void flush() throw IOException;
  76.  
  77. abstract public void close() throw IOException;
  78. }

可以看到在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. jquery validate ajax 验证重复的2种方法

    转载自:http://blog.51yip.com/jsjquery/1484.html jquery validate 经过这种多年的改良,已经很完善了.它能满足80%的验证需要,如果validat ...

  2. 深入探索C++对象模型(七)

    站在对象模型的尖端(On the Cusp of the Object Model) Template 下面是有关template的三个主要讨论方向: template的声明,基本上来说就是当你声明一 ...

  3. JSOI2008 小店购物

    https://www.luogu.org/problem/show?pid=2792 题目背景 JSOI集训队的队员发现,在他们经常活动的集训地,有一个小店因为其丰富的经营优惠方案深受附近居民的青睐 ...

  4. bzoj 1901 Dynamic Rankings (树状数组套线段树)

    1901: Zju2112 Dynamic Rankings Time Limit: 10 Sec  Memory Limit: 128 MB Description 给定一个含有n个数的序列a[1] ...

  5. 用英文写Email的注意事项

  6. Assert 的用法

    Assert Assert是断言的意思,头文件为assert.h, assert是一个宏 功 能: 测试一个条件并可能使程序终止 用 法: void assert(int test); 在单元测试中经 ...

  7. Html符号

  8. POJ 3233 Matrix Power Series (矩阵快速幂)

    题目链接 Description Given a n × n matrix A and a positive integer k, find the sum S = A + A^2 + A^3 + - ...

  9. 生成验证码tp

    js里拼接随机数 页面上链接 去掉后缀名

  10. 深入理解Spring系列之一:开篇

    转载 https://mp.weixin.qq.com/s?__biz=MzI0NjUxNTY5Nw==&mid=2247483810&idx=1&sn=a2df14fdb63 ...