Java CharArrayWriter类的简介说明

java-教程王 Java教程 发布时间:2021-11-30 11:51:26 阅读数:2235 1
下文笔者将讲述CharArrayWriter类的简介说明,如下所示:
CharArrayReader类的功能:
    用于写入数据符
    可实现将流输出到数组
 

CharArrayWriter类的构造函数

CharArrayWriter()
CharArrayWriter(int initialSize)

CharArrayWriter类的方法

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)
例:
public static void main(String[] args) {
    String s = "java265.com是我最爱的网站";
    char[] dst = new char[s.length()]; //定义字符串
    s.getChars(0, s.length(), dst, 0); //将字符串转变为字符数组
    CharArrayReader charArrayReader = new CharArrayReader(dst); //输入流实例
    CharArrayWriter charArrayWriter = new CharArrayWriter(); //输出流实例
    try {
        while (charArrayReader.ready()) {
            charArrayWriter.write(charArrayReader.read()); //将输入流的内容写入输出流
        }
    } catch(IOException e) {
        e.printStackTrace();
    }
    //将输出流转换为字符数组
    char[] temp_char = charArrayWriter.toCharArray();
    System.out.println("输出字符数组:");
    //将输出流转换为字符串
    String temp_str = charArrayWriter.toString();
    for (int i = 0; i < temp_char.length; i++) {
        System.out.print(temp_char[i]);
    }
    System.out.println();
    System.out.println("输出字符串:");
    System.out.println(temp_str);
}
版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

本文链接: https://www.Java265.com/JavaCourse/202111/1951.html

最近发表

热门文章

好文推荐

Java265.com

https://www.java265.com

站长统计|粤ICP备14097017号-3

Powered By Java265.com信息维护小组

使用手机扫描二维码

关注我们看更多资讯

java爱好者