java.io.writer API 以及 源码解读
2017-05-25 14:57
435 查看
声明 我看的是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的源码。
可以看到在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 继承了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.BufferedWriter API 以及源码解读
- OutputStreamWriter API 以及源码解读
- java IO之字节流和字符流-Reader和Writer以及实现文件复制拷贝
- JavaIO(3):Writer,OutputStreamWriter和FileWriter源码分析
- Java IO:CharArrayWriter使用及源码分析
- Java_io体系之Writer、Reader简介、走进源码——12
- java IO,伪异步IO以及NIO网络编程 简单实现源码以及区别
- java IO之字节流和字符流-Reader和Writer以及实现文件复制拷贝
- 如何轻松 查看Java API ,以及Android API源码(免去翻墙的痛苦)
- Java_io体系之CharArrayReader、CharArrayWriter简介、走进源码及示例——13
- jQuery.API源码深入剖析以及应用实现(3) - 选择器篇(上)
- jQuery.API源码深入剖析以及应用实现(1) - 核心函数篇
- jQuery.API源码深入剖析以及应用实现(1) - 核心函数篇
- jQuery.API源码深入剖析以及应用实现(4) - 选择器篇(下)
- JAVA中API的学习先后,以及查询网址和JAVA技术论坛
- 软件包 java.io 的分层结构(http://gceclub.sun.com.cn/Java_Docs/html/zh_CN/api/java/io/package-tree.html)
- jQuery.API源码深入剖析以及应用实现(1) - 核心函数篇
- 详细解读java IO
- jQuery.API源码深入剖析以及应用实现(3) - 选择器篇(上)
- JR 精品文章 - Java??2源码解读1:java.util.ArrayList??(版本:2.0)