public class CharArrayWriter
extends Writer
java.lang.Object | ||
↳ | java.io.Writer | |
↳ | java.io.CharArrayWriter |
这个类实现了可以用作Writer的字符缓冲区。 数据写入流时缓冲区会自动增长。 数据可以使用toCharArray()和toString()来检索。
注意:对此类调用close()不起作用,可以在流关闭后调用此类的方法,而不产生IOException。
Fields |
|
---|---|
protected char[] |
buf 数据存储的缓冲区。 |
protected int |
count 缓冲区中的字符数。 |
Inherited fields |
---|
From class java.io.Writer
|
Public constructors |
|
---|---|
CharArrayWriter() 创建一个新的CharArrayWriter。 |
|
CharArrayWriter(int initialSize) 用指定的初始大小创建一个新的CharArrayWriter。 |
Public methods |
|
---|---|
CharArrayWriter |
append(char c) 将指定的字符附加到此作者。 |
CharArrayWriter |
append(CharSequence csq, int start, int end) 将指定字符序列的子序列附加到此作者。 |
CharArrayWriter |
append(CharSequence csq) 将指定的字符序列追加到此作者。 |
void |
close() 关闭流。 |
void |
flush() 冲洗流。 |
void |
reset() 重置缓冲区,以便您可以再次使用它而不会丢弃已分配的缓冲区。 |
int |
size() 返回缓冲区的当前大小。 |
char[] |
toCharArray() 返回输入数据的副本。 |
String |
toString() 将输入数据转换为字符串。 |
void |
write(int c) 将字符写入缓冲区。 |
void |
write(String str, int off, int len) 将一部分字符串写入缓冲区。 |
void |
write(char[] c, int off, int len) 将字符写入缓冲区。 |
void |
writeTo(Writer out) 将缓冲区的内容写入另一个字符流。 |
Inherited methods |
|
---|---|
From class java.io.Writer
|
|
From class java.lang.Object
|
|
From interface java.lang.Appendable
|
|
From interface java.io.Closeable
|
|
From interface java.io.Flushable
|
|
From interface java.lang.AutoCloseable
|
CharArrayWriter (int initialSize)
用指定的初始大小创建一个新的CharArrayWriter。
Parameters | |
---|---|
initialSize |
int : an int specifying the initial buffer size. |
Throws | |
---|---|
IllegalArgumentException |
if initialSize is negative |
CharArrayWriter append (char c)
将指定的字符附加到此作者。
表单 out.append(c)的此方法的调用的行为与调用完全相同
out.write(c)
Parameters | |
---|---|
c |
char : The 16-bit character to append |
Returns | |
---|---|
CharArrayWriter |
This writer |
CharArrayWriter append (CharSequence csq, int start, int end)
将指定字符序列的子序列附加到此作者。
形式的这种方法的调用时 out.append(csq, start, end) csq不是 null,行为以完全相同的方式调用
out.write(csq.subSequence(start, end).toString())
Parameters | |
---|---|
csq |
CharSequence : The character sequence from which a subsequence will be appended. If csq is null, then characters will be appended as if csq contained the four characters "null". |
start |
int : The index of the first character in the subsequence |
end |
int : The index of the character following the last character in the subsequence |
Returns | |
---|---|
CharArrayWriter |
This writer |
Throws | |
---|---|
IndexOutOfBoundsException |
If start or end are negative, start is greater than end, or end is greater than csq.length() |
CharArrayWriter append (CharSequence csq)
将指定的字符序列追加到此作者。
调用表单 out.append(csq)的此方法的行为与调用完全相同
out.write(csq.toString())
取决于toString字符序列csq本说明书中,整个序列可以不追加。 例如,调用字符缓冲区的toString方法将返回一个子序列,其内容取决于缓冲区的位置和限制。
Parameters | |
---|---|
csq |
CharSequence : The character sequence to append. If csq is null, then the four characters "null" are appended to this writer. |
Returns | |
---|---|
CharArrayWriter |
This writer |
int size ()
返回缓冲区的当前大小。
Returns | |
---|---|
int |
an int representing the current size of the buffer. |
char[] toCharArray ()
返回输入数据的副本。
Returns | |
---|---|
char[] |
an array of chars copied from the input data. |
void write (int c)
将字符写入缓冲区。
Parameters | |
---|---|
c |
int : int specifying a character to be written |
void write (String str, int off, int len)
将一部分字符串写入缓冲区。
Parameters | |
---|---|
str |
String : String to be written from |
off |
int : Offset from which to start reading characters |
len |
int : Number of characters to be written |
void write (char[] c, int off, int len)
将字符写入缓冲区。
Parameters | |
---|---|
c |
char : the data to be written |
off |
int : the start offset in the data |
len |
int : the number of chars that are written |
void writeTo (Writer out)
将缓冲区的内容写入另一个字符流。
Parameters | |
---|---|
out |
Writer : the output stream to write to |
Throws | |
---|---|
IOException |
If an I/O error occurs. |