java.lang.Object
java.io.Writer
java.io.CharArrayWriter
- 所有已实现的接口:
-
Closeable,Flushable,Appendable,AutoCloseable
该类实现了一个可以用作Writer的字符缓冲区。当数据被写入流时,缓冲区会自动增长。数据可以使用toCharArray()和toString()方法检索。
注意: 在此类上调用close()方法没有效果,而且在流关闭后仍然可以调用此类的方法而不会生成IOException。
- 自从:
- 1.1
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescription创建一个新的CharArrayWriter。CharArrayWriter(int initialSize) 创建一个具有指定初始大小的新CharArrayWriter。 -
Method Summary
Modifier and TypeMethodDescriptionappend(char c) 将指定的字符追加到此writer。append(CharSequence csq) 将指定的字符序列追加到此writer。append(CharSequence csq, int start, int end) 将指定字符序列的子序列追加到此writer。voidclose()关闭流。voidflush()刷新流。voidreset()重置缓冲区,以便您可以再次使用它而不丢弃已分配的缓冲区。intsize()返回缓冲区的当前大小。char[]返回输入数据的副本。toString()将输入数据转换为字符串。voidwrite(char[] c, int off, int len) 将字符写入缓冲区。voidwrite(int c) 将字符写入缓冲区。void将字符串的一部分写入缓冲区。void将缓冲区的内容写入另一个字符流。Methods declared in class java.io.Writer
nullWriter, write, write
-
Field Details
-
buf
protected char[] buf存储数据的缓冲区。 -
count
protected int count缓冲区中的字符数。
-
-
Constructor Details
-
CharArrayWriter
public CharArrayWriter()创建一个新的CharArrayWriter。 -
CharArrayWriter
public CharArrayWriter(int initialSize) 创建一个具有指定初始大小的新CharArrayWriter。- 参数:
-
initialSize- 指定初始缓冲区大小的整数。 - 抛出:
-
IllegalArgumentException- 如果initialSize为负
-
-
Method Details
-
write
public void write(int c) 将字符写入缓冲区。 -
write
public void write(char[] c, int off, int len) 将字符写入缓冲区。- 指定为:
-
write在类Writer - 参数:
-
c- 要写入的数据 -
off- 数据中的起始偏移量 -
len- 要写入的字符数 - 抛出:
-
IndexOutOfBoundsException- 如果off为负,或len为负,或off + len为负或大于给定数组的长度
-
write
将字符串的一部分写入缓冲区。- 覆盖:
-
write在类Writer - 参数:
-
str- 要从中写入的字符串 -
off- 开始读取字符的偏移量 -
len- 要写入的字符数 - 抛出:
-
IndexOutOfBoundsException- 如果off为负,或len为负,或off + len为负或大于给定字符串的长度
-
writeTo
将缓冲区的内容写入另一个字符流。- 参数:
-
out- 要写入的输出流 - 抛出:
-
IOException- 如果发生I/O错误。
-
append
将指定的字符序列追加到此writer。以
out.append(csq)形式调用此方法的行为与调用方式完全相同out.write(csq.toString())根据字符序列
csq的toString规范,可能不会追加整个序列。例如,调用字符缓冲区的toString方法将返回一个内容取决于缓冲区位置和限制的子序列。- 指定为:
-
append在接口Appendable - 覆盖:
-
append在类Writer - 参数:
-
csq- 要追加的字符序列。如果csq为null,则将四个字符"null"追加到此writer。 - 返回:
- 此writer
- 自从:
- 1.5
-
append
将指定字符序列的子序列追加到此writer。以
out.append(csq, start, end)形式调用此方法时,csq不为null,行为与调用方式完全相同out.write(csq.subSequence(start, end).toString())- 指定为:
-
append在接口Appendable - 覆盖:
-
append在类Writer - 参数:
-
csq- 要追加子序列的字符序列。如果csq为null,则将字符追加,就好像csq包含四个字符"null"。 -
start- 子序列中第一个字符的索引 -
end- 子序列中最后一个字符的后一个字符的索引 - 返回:
- 此writer
- 抛出:
-
IndexOutOfBoundsException- 如果start或end为负,start大于end,或end大于csq.length() - 自从:
- 1.5
-
append
将指定的字符追加到此writer。以
out.append(c)形式调用此方法的行为与调用方式完全相同out.write(c)- 指定为:
-
append在接口Appendable - 覆盖:
-
append在类Writer - 参数:
-
c- 要追加的16位字符 - 返回:
- 此writer
- 自从:
- 1.5
-
reset
public void reset()重置缓冲区,以便您可以再次使用它而不丢弃已分配的缓冲区。 -
toCharArray
public char[] toCharArray()返回输入数据的副本。- 返回:
- 从输入数据复制的字符数组。
-
size
public int size()返回缓冲区的当前大小。- 返回:
- 代表缓冲区当前大小的整数。
-
toString
将输入数据转换为字符串。 -
flush
public void flush()刷新流。CharArrayWriter的flush方法不执行任何操作。 -
close
public void close()关闭流。此方法不释放缓冲区,因为其内容可能仍然需要。注意: 在此类中调用此方法将不会产生任何效果。
-