本章,我们学习CharArrayWriter。学习时,我们先对CharArrayWriter有个大致了解,然后深入了解一下它的源码,最后通过示例来掌握它的用法。

转载请注明出处:http://www.cnblogs.com/skywang12345/p/io_19.html 

更多内容请参考:Java io系列 "目录" 

CharArrayWriter 介绍

CharArrayReader 用于写入数据符,它继承于Writer。操作的数据是以字符为单位!

CharArrayWriter 函数列表

CharArrayWriter()
CharArrayWriter(int initialSize)

CharArrayWriter     append(CharSequence csq, int start, int end)
CharArrayWriter     append(char c)
CharArrayWriter     append(CharSequence csq)
void     close()
void     flush()
void     reset()
int     size()
char[]     toCharArray()
String     toString()
void     write(char[] buffer, int offset, int len)
void     write(int oneChar)
void     write(String str, int offset, int count)
void     writeTo(Writer out)

 

Writer和CharArrayWriter源码分析

Writer是CharArrayWriter的父类,我们先看看Writer的源码,然后再学CharArrayWriter的源码。

1. Writer源码分析(基于jdk1.7.40)

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

相关文章:

  • 2021-07-03
  • 2022-12-23
  • 2021-08-19
  • 2021-11-14
猜你喜欢
  • 2022-02-19
  • 2022-12-23
  • 2022-01-07
相关资源
相似解决方案