- java.lang.Object
-
- java.io.Writer
-
- java.io.CharArrayWriter
-
- All Implemented Interfaces:
-
Closeable,Flushable,Appendable,AutoCloseable
public class CharArrayWriter extends Writer
该类实现了可以用作Writer的字符缓冲区。 当数据写入流时,缓冲区会自动增长。 可以使用toCharArray()和toString()检索数据。注意:在此类上调用close()没有任何效果,并且流已关闭而不生成IOException时可以调用此类的方法。
- 从以下版本开始:
- 1.1
-
-
构造方法摘要
构造方法 Constructor 描述 CharArrayWriter()创建一个新的CharArrayWriter。CharArrayWriter(int initialSize)用指定的初始大小创建一个新的CharArrayWriter。
-
方法摘要
所有方法 接口方法 具体的方法 Modifier and Type 方法 描述 CharArrayWriterappend(char c)将指定的字符附加到此作者。CharArrayWriterappend(CharSequence csq)将指定的字符序列附加到此作者。CharArrayWriterappend(CharSequence csq, int start, int end)将指定字符序列的子序列附加到此作者。voidclose()关闭流。voidflush()冲洗流。voidreset()重置缓冲区,以便您可以再次使用它,而不会丢弃已经分配的缓冲区。intsize()返回缓冲区的当前大小。char[]toCharArray()返回输入数据的副本。StringtoString()将输入数据转换为字符串。voidwrite(char[] c, int off, int len)将字符写入缓冲区。voidwrite(int c)将一个字符写入缓冲区。voidwrite(String str, int off, int len)将一部分字符串写入缓冲区。voidwriteTo(Writer out)将缓冲区的内容写入另一个字符流。
-
-
-
构造方法详细信息
-
CharArrayWriter
public CharArrayWriter()
创建一个新的CharArrayWriter。
-
CharArrayWriter
public CharArrayWriter(int initialSize)
用指定的初始大小创建一个新的CharArrayWriter。- 参数
-
initialSize- 指定初始缓冲区大小的int。 - 异常
-
IllegalArgumentException- 如果initialSize为负数
-
-
方法详细信息
-
write
public void write(char[] c, int off, int len)将字符写入缓冲区。- Specified by:
-
write在Writer - 参数
-
c- 要写入的数据 -
off- 数据中的起始偏移量 -
len- 写入的字符数 - 异常
-
IndexOutOfBoundsException- 如果off为负数,或len为负数,或off + len为负数或大于给定数组的长度
-
write
public void write(String str, int off, int len)
将一部分字符串写入缓冲区。- 重写:
-
write在Writer - 参数
-
str- 要写入的字符串 -
off- 开始读取字符的偏移量 -
len- 要写入的字符数 - 异常
-
IndexOutOfBoundsException- 如果off为负数,或len为负数,或off + len为负数或大于给定字符串的长度
-
writeTo
public void writeTo(Writer out) throws IOException
将缓冲区的内容写入另一个字符流。- 参数
-
out- 要写入的输出流 - 异常
-
IOException- 如果发生I / O错误。
-
append
public CharArrayWriter append(CharSequence csq)
将指定的字符序列附加到此作者。这种
out.append(csq)形式的方法的调用与调用的方式完全相同out.write(csq.toString())取决于规范
toString字符序列csq,整个序列可以不追加。 例如,调用字符缓冲区的toString方法将返回一个子序列,其内容取决于缓冲区的位置和限制。- Specified by:
-
append接口Appendable - 重写:
-
append在Writer - 参数
-
csq- 要附加的字符序列。 如果csq为null,则该"null"器附加四个字符"null"。 - 结果
- 这位作家
- 从以下版本开始:
- 1.5
-
append
public CharArrayWriter append(CharSequence csq, int start, int end)
将指定字符序列的子序列附加到此作者。形式的这种方法的调用
out.append(csq, start, end)时csq不是null,行为以完全相同的方式调用out.write(csq.subSequence(start, end).toString())- Specified by:
-
append在接口Appendable - 重写:
-
append在Writer - 参数
-
csq- 附加子序列的字符序列。 如果csq是null,那么字符将被追加,就像csq包含四个字符"null"。 -
start- 子序列中第一个字符的索引 -
end- 子序列中最后一个字符后面的字符的索引 - 结果
- 这位作家
- 异常
-
IndexOutOfBoundsException- 如果start或end为负数,则start大于end,或end大于csq.length() - 从以下版本开始:
- 1.5
-
append
public CharArrayWriter append(char c)
将指定的字符附加到此作者。调用此方法的形式为
out.append(c)行为与调用的方式完全相同out.write(c)- Specified by:
-
append在接口Appendable - 重写:
-
append在Writer - 参数
-
c- 要追加的16位字符 - 结果
- 这位作家
- 从以下版本开始:
- 1.5
-
reset
public void reset()
重置缓冲区,以便您可以再次使用它,而不会丢弃已经分配的缓冲区。
-
toCharArray
public char[] toCharArray()
返回输入数据的副本。- 结果
- 一组从输入数据复制的字符。
-
size
public int size()
返回缓冲区的当前大小。- 结果
- 一个表示当前缓冲区大小的int。
-
-