1、概述

public interface Appendable
能够被添加 char 序列和值的对象。如果某个类的实例打算接收取自 Formatter 的格式化输出,那么该类必须实现 Appendable 接口。
要添加的字符应该是有效的 Unicode 字符,正如 Unicode Character Representation 中描述的那样。注意,增补字符可能由多个 16 位 char 值组成。
Appendable 对于多线程访问而言没必要是安全的。线程安全由扩展和实现此接口的类负责。
由于此接口可能由具有不同的错误处理风格的现有类实现,所以无法保证错误不会传播给调用者

 

2、已知实现的子类

 1 BufferedWriter, 
 2 CharArrayWriter,
 3 CharBuffer, 
 4 FileWriter, 
 5 FilterWriter,  
 6 LogStream, 
 7 OutputStreamWriter, 
 8 PipedWriter, 
 9 PrintStream, 
10 PrintWriter, 
11 StringBuffer, 
12 StringBuilder, 
13 StringWriter, 
14 Writer

 

3、源码

 1 public interface Appendable{
 2         /**
 3          * 向此Appendable添加指定的字符序列。
 4          * 有时可能不会全部添加真个序列,这取决于使用哪个类来实现字符序列 csq。
 5          * 例如:如果csq是java.nio.CharBuffer的实例,则通过缓冲区的位置和限制来定义要添加的子序列。
 6          * @param  csq
 7          * 要添加的字符串序列。如果 csq 为 null,则向该 Appendable 添加四个字符 "null"。 
 8          * @return  此Appendable的引用
 9          * @throws  IOException
10          *          如果发生I/O错误
11          */
12         Appendable append(CharSequence csq) throws IOException;
13         /**
14          * 向此Appendable添加指定的字符序列的子序列。
15          * 当csq不为null的时候,调用 out.append(csq, start, end) 方法
16          * 和调用 out.append(csq.subSequence(start, end))方法完全相同
17          *
18          * @param  csq
19          * 子序列将被添加的字符序列。如果 csq 为 null,则添加四个字符 "null",就好像 csq 包含这些字符一样。
20          * @param  start
21          * 子序列中第一个字符索引
22          * @param  end
23          * 在子序列中最后一个字符后面的字符的索引。
24          * @return 此Appendable的引用
25          * @throws  IndexOutOfBoundsException
26          * 如果 start 或 end 为负,以及 start 大于 end 或者 end 大于 csq.length() 
27          * @throws  IOException
28          *        如果发生I/O错误
29          */
30         Appendable append(CharSequence csq, int start, int end) throws IOException;
31         //向此 Appendable 添加指定字符。
32         Appendable append(char c) throws IOException;
33 
34     }

 

4、例子1

 

5、例子2