java io系列19之 CharArrayWriter(字符数组输出流)
本章,我们学习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)
package java.io; public abstract class Writer implements Appendable, Closeable, Flushable { private char[] writeBuffer; private final int writeBufferSize = 1024; protected 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) throws IOException {
synchronized (lock) {
if (writeBuffer == null){
writeBuffer = new char[writeBufferSize];
}
writeBuffer[0] = (char) c;
write(writeBuffer, 0, 1);
}
} public void write(char cbuf[]) throws IOException {
write(cbuf, 0, cbuf.length);
} abstract public void write(char cbuf[], int off, int len) throws IOException; public void write(String str) throws IOException {
write(str, 0, str.length());
} public void write(String str, int off, int len) throws IOException {
synchronized (lock) {
char cbuf[];
if (len <= writeBufferSize) {
if (writeBuffer == null) {
writeBuffer = new char[writeBufferSize];
}
cbuf = writeBuffer;
} else { // Don't permanently allocate very large buffers.
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) throws IOException {
CharSequence cs = (csq == null ? "null" : csq);
write(cs.subSequence(start, end).toString());
return this;
} public Writer append(char c) throws IOException {
write(c);
return this;
} abstract public void flush() throws IOException; abstract public void close() throws IOException;
}
2. CharArrayWriter 源码分析(基于jdk1.7.40)
package java.io; import java.util.Arrays; public class CharArrayWriter extends Writer {
// 字符数组缓冲
protected char buf[]; // 下一个字符的写入位置
protected int count; // 构造函数:默认缓冲区大小是32
public CharArrayWriter() {
this(32);
} // 构造函数:指定缓冲区大小是initialSize
public CharArrayWriter(int initialSize) {
if (initialSize < 0) {
throw new IllegalArgumentException("Negative initial size: "
+ initialSize);
}
buf = new char[initialSize];
} // 写入一个字符c到CharArrayWriter中
public void write(int c) {
synchronized (lock) {
int newcount = count + 1;
if (newcount > buf.length) {
buf = Arrays.copyOf(buf, Math.max(buf.length << 1, newcount));
}
buf[count] = (char)c;
count = newcount;
}
} // 写入字符数组c到CharArrayWriter中。off是“字符数组b中的起始写入位置”,len是写入的长度
public void write(char c[], int off, int len) {
if ((off < 0) || (off > c.length) || (len < 0) ||
((off + len) > c.length) || ((off + len) < 0)) {
throw new IndexOutOfBoundsException();
} else if (len == 0) {
return;
}
synchronized (lock) {
int newcount = count + len;
if (newcount > buf.length) {
buf = Arrays.copyOf(buf, Math.max(buf.length << 1, newcount));
}
System.arraycopy(c, off, buf, count, len);
count = newcount;
}
} // 写入字符串str到CharArrayWriter中。off是“字符串的起始写入位置”,len是写入的长度
public void write(String str, int off, int len) {
synchronized (lock) {
int newcount = count + len;
if (newcount > buf.length) {
buf = Arrays.copyOf(buf, Math.max(buf.length << 1, newcount));
}
str.getChars(off, off + len, buf, count);
count = newcount;
}
} // 将CharArrayWriter写入到“Writer对象out”中
public void writeTo(Writer out) throws IOException {
synchronized (lock) {
out.write(buf, 0, count);
}
} // 将csq写入到CharArrayWriter中
// 注意:该函数返回CharArrayWriter对象
public CharArrayWriter append(CharSequence csq) {
String s = (csq == null ? "null" : csq.toString());
write(s, 0, s.length());
return this;
} // 将csq从start开始(包括)到end结束(不包括)的数据,写入到CharArrayWriter中。
// 注意:该函数返回CharArrayWriter对象!
public CharArrayWriter append(CharSequence csq, int start, int end) {
String s = (csq == null ? "null" : csq).subSequence(start, end).toString();
write(s, 0, s.length());
return this;
} // 将字符c追加到CharArrayWriter中!
// 注意:它与write(int c)的区别。append(char c)会返回CharArrayWriter对象。
public CharArrayWriter append(char c) {
write(c);
return this;
} // 重置
public void reset() {
count = 0;
} // 将CharArrayWriter的全部数据对应的char[]返回
public char toCharArray()[] {
synchronized (lock) {
return Arrays.copyOf(buf, count);
}
} // 返回CharArrayWriter的大小
public int size() {
return count;
} public String toString() {
synchronized (lock) {
return new String(buf, 0, count);
}
} public void flush() { } public void close() { }
}
说明:
CharArrayWriter实际上是将数据写入到“字符数组”中去。
(01) 通过CharArrayWriter()创建的CharArrayWriter对应的字符数组大小是32。
(02) 通过CharArrayWriter(int size) 创建的CharArrayWriter对应的字符数组大小是size。
(03) write(int oneChar)的作用将int类型的oneChar换成char类型,然后写入到CharArrayWriter中。
(04) write(char[] buffer, int offset, int len) 是将字符数组buffer写入到输出流中,offset是从buffer中读取数据的起始偏移位置,len是读取的长度。
(05) write(String str, int offset, int count) 是将字符串str写入到输出流中,offset是从str中读取数据的起始位置,count是读取的长度。
(06) append(char c)的作用将char类型的c写入到CharArrayWriter中,然后返回CharArrayWriter对象。
注意:append(char c)与write(int c)都是将单个字符写入到CharArrayWriter中。它们的区别是,append(char c)会返回CharArrayWriter对象,但是write(int c)返回void。
(07) append(CharSequence csq, int start, int end)的作用将csq从start开始(包括)到end结束(不包括)的数据,写入到CharArrayWriter中。
注意:该函数返回CharArrayWriter对象!
(08) append(CharSequence csq)的作用将csq写入到CharArrayWriter中。
注意:该函数返回CharArrayWriter对象!
(09) writeTo(OutputStream out) 将该“字符数组输出流”的数据全部写入到“输出流out”中。
示例代码
关于CharArrayWriter中API的详细用法,参考示例代码(CharArrayWriterTest.java):
import java.io.CharArrayReader;
import java.io.CharArrayWriter;
import java.io.IOException; /**
* CharArrayWriter 测试程序
*
* @author skywang
*/
public class CharArrayWriterTest { private static final int LEN = 5;
// 对应英文字母“abcdefghijklmnopqrstuvwxyz”
private static final char[] ArrayLetters = new char[] {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'}; public static void main(String[] args) { tesCharArrayWriter() ;
} /**
* CharArrayWriter的API测试函数
*/
private static void tesCharArrayWriter() {
try {
// 创建CharArrayWriter字符流
CharArrayWriter caw = new CharArrayWriter(); // 写入“A”个字符
caw.write('A');
// 写入字符串“BC”个字符
caw.write("BC");
//System.out.printf("caw=%s\n", caw);
// 将ArrayLetters数组中从“3”开始的后5个字符(defgh)写入到caw中。
caw.write(ArrayLetters, 3, 5);
//System.out.printf("caw=%s\n", caw); // (01) 写入字符0
// (02) 然后接着写入“123456789”
// (03) 再接着写入ArrayLetters中第8-12个字符(ijkl)
caw.append('0').append("123456789").append(String.valueOf(ArrayLetters), 8, 12); System.out.printf("caw=%s\n", caw); // 计算长度
int size = caw.size();
System.out.printf("size=%s\n", size); // 转换成byte[]数组
char[] buf = caw.toCharArray();
System.out.printf("buf=%s\n", String.valueOf(buf)); // 将caw写入到另一个输出流中
CharArrayWriter caw2 = new CharArrayWriter();
caw.writeTo(caw2);
System.out.printf("caw2=%s\n", caw2);
} catch (IOException e) {
e.printStackTrace();
}
}
}
运行结果:
caw=ABCdefgh0123456789ijkl
size=22
buf=ABCdefgh0123456789ijkl
caw2=ABCdefgh0123456789ijkl
java io系列19之 CharArrayWriter(字符数组输出流)的更多相关文章
- java io系列24之 BufferedWriter(字符缓冲输出流)
转载请注明出处:http://www.cnblogs.com/skywang12345/p/io_24.html 更多内容请参考:java io系列01之 "目录" Buffere ...
- java io系列25之 PrintWriter (字符打印输出流)
更多内容请参考:java io系列01之 "目录" PrintWriter 介绍 PrintWriter 是字符类型的打印输出流,它继承于Writer.PrintStream 用于 ...
- java io系列18之 CharArrayReader(字符数组输入流)
从本章开始,我们开始对java io中的“字符流”进行学习.首先,要学习的是CharArrayReader.学习时,我们先对CharArrayReader有个大致了解,然后深入了解一下它的源码,最后通 ...
- java io系列23之 BufferedReader(字符缓冲输入流)
转载请注明出处:http://www.cnblogs.com/skywang12345/p/io_23.html 更多内容请参考:java io系列01之 "目录" Buffere ...
- Java IO学习--(五)字节和字符数组
内容列表 从InputStream或者Reader中读入数组 从OutputStream或者Writer中写数组 在java中常用字节和字符数组在应用中临时存储数据.而这些数组又是通常的数据读取来源或 ...
- java io系列01之 "目录"
java io 系列目录如下: 01. java io系列01之 "目录" 02. java io系列02之 ByteArrayInputStream的简介,源码分析和示例(包括 ...
- java io系列
java io系列01之 "目录" java io系列02之 ByteArrayInputStream的简介,源码分析和示例(包括InputStream) java io系列03之 ...
- java io系列16之 PrintStream(打印输出流)详解
本章介绍PrintStream以及 它与DataOutputStream的区别.我们先对PrintStream有个大致认识,然后再深入学习它的源码,最后通过示例加深对它的了解. 转载请注明出处:htt ...
- java io系列15之 DataOutputStream(数据输出流)的认知、源码和示例
本章介绍DataOutputStream.我们先对DataOutputStream有个大致认识,然后再深入学习它的源码,最后通过示例加深对它的了解. 转载请注明出处:http://www.cnblog ...
随机推荐
- ng-click 发两次ajax请求的原因及解决方法
http://blog.csdn.net/anmo/article/details/17083125
- kubernetes 构架
- [Codeforces266E]More Queries to Array...——线段树
题目链接: Codeforces266E 题目大意:给出一个序列$a$,要求完成$Q$次操作,操作分为两种:1.$l,r,x$,将$[l,r]$的数都变为$x$.2.$l,r,k$,求$\sum\li ...
- BZOJ1001 洛谷4001 [BJOI2006]狼抓兔子 题解
题目 这个题目有多种解法,这个题也是一个比较经典的题了,正是因为他的多样的做法,这个题主要难在建图和优化,因为这是一个网格图,所以spfa肯定过不去,所以用最短路解法的话,只能用dij,而网络流也是要 ...
- 个人整理的数组splay板子,指针的写的太丑了就不放了。。
splay的板子.. 由于被LCT榨干了..所以昨天去学了数组版的splay,现在整理一下板子.. 以BZOJ3224和3223为例题..暂时只有这些,序列的话等有时间把维修序列给弄上来!! BZOJ ...
- 【BZOJ4653】【NOI2016】区间 线段树
题目大意 数轴上有\(n\)个闭区间\([l_1,r_1],[l_2,r_2],\ldots,[l_n,r_n]\),你要选出\(m\)个区间,使得存在一个\(x\),对于每个选出的区间\([l_i, ...
- 【BZOJ3669】【NOI2014】魔法森林 LCT
题目描述 给你一个\(n\)个点\(m\)条边的图,每条边有两个边权\(a,b\).请你找出从\(1\)到\(n\)一条路径,使得这条路径上边权\(a\)的最大值\(+\)边权\(b\)的最大值最小. ...
- FWT模板(洛谷P4717 【模板】快速沃尔什变换)(FWT)
洛谷题目传送门 只是一个经过了蛇皮压行的模板... 总结?%%%yyb%%% #include<bits/stdc++.h> #define LL long long #define RG ...
- 扩展资源服务器解决oauth2 性能瓶颈
OAuth用户携带token 请求资源服务器资源服务器拦截器 携带token 去认证服务器 调用tokenstore 对token 合法性校验资源服务器拿到token,默认只会含有用户名信息通过用户名 ...
- 【LOJ#6074】子序列(动态规划)
[LOJ#6074]子序列(动态规划) 题面 LOJ 题解 考虑一个暴力\(dp\). 设\(f[i][c]\)表示当前在第\(i\)位,并且以\(c\)结尾的子序列个数. 那么假设当前位为\(a\) ...