Java的字符读写类CharArrayReader和CharArrayWriter使用示例

时间:2022-01-23 17:57:15

CharArrayReader
CharArrayReader 是字符数组输入流。它和ByteArrayInputStream类似,只不过ByteArrayInputStream是字节数组输入流,而CharArray是字符数组输入流。CharArrayReader 是用于读取字符数组,它继承于Reader。操作的数据是以字符为单位!
CharArrayReader 函数列表:

?
1
2
3
4
5
6
7
8
9
10
11
CharArrayReader(char[] buf)
CharArrayReader(char[] buf, int offset, int length)
 
void   close()
void   mark(int readLimit)
boolean  markSupported()
int    read()
int    read(char[] buffer, int offset, int len)
boolean  ready()
void   reset()
long   skip(long charCount)

示例代码:
关于CharArrayReader中API的详细用法,参考示例代码(CharArrayReaderTest.java):

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import java.io.CharArrayReader;
import java.io.CharArrayWriter;
import java.io.IOException;
 
public class CharArrayReaderTest {
 
  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) {
    tesCharArrayReader() ;
  }
 
  /**
   * CharArrayReader的API测试函数
   */
  private static void tesCharArrayReader() {
    try {
      // 创建CharArrayReader字符流,内容是ArrayLetters数组
      CharArrayReader car = new CharArrayReader(ArrayLetters);
 
      // 从字符数组流中读取5个字符
      for (int i=0; i<LEN; i++) {
        // 若能继续读取下一个字符,则读取下一个字符
        if (car.ready() == true) {
          // 读取“字符流的下一个字符”
          char tmp = (char)car.read();
          System.out.printf("%d : %c\n", i, tmp);
        }
      }
 
      // 若“该字符流”不支持标记功能,则直接退出
      if (!car.markSupported()) {
        System.out.println("make not supported!");
        return ;
      }
 
      // 标记“字符流中下一个被读取的位置”。即--标记“f”,因为因为前面已经读取了5个字符,所以下一个被读取的位置是第6个字符”
      // (01), CharArrayReader类的mark(0)函数中的“参数0”是没有实际意义的。
      // (02), mark()与reset()是配套的,reset()会将“字符流中下一个被读取的位置”重置为“mark()中所保存的位置”
      car.mark(0);
 
      // 跳过5个字符。跳过5个字符后,字符流中下一个被读取的值应该是“k”。
      car.skip(5);
 
      // 从字符流中读取5个数据。即读取“klmno”
      char[] buf = new char[LEN];
      car.read(buf, 0, LEN);
      System.out.printf("buf=%s\n", String.valueOf(buf));
 
      // 重置“字符流”:即,将“字符流中下一个被读取的位置”重置到“mark()所标记的位置”,即f。
      car.reset();
      // 从“重置后的字符流”中读取5个字符到buf中。即读取“fghij”
      car.read(buf, 0, LEN);
      System.out.printf("buf=%s\n", String.valueOf(buf));
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

运行结果:

?
1
2
3
4
5
6
7
0 : a
1 : b
2 : c
3 : d
4 : e
buf=klmno
buf=fghij

CharArrayWriter
CharArrayReader 用于写入数据符,它继承于Writer。操作的数据是以字符为单位!
CharArrayWriter 函数列表

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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)

示例代码:
关于CharArrayWriter中API的详细用法,参考示例代码(CharArrayWriterTest.java):

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import java.io.CharArrayReader;
import java.io.CharArrayWriter;
import java.io.IOException;
 
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();
    }
  }
}

运行结果:

?
1
2
3
4
caw=ABCdefgh0123456789ijkl
size=22
buf=ABCdefgh0123456789ijkl
caw2=ABCdefgh0123456789ijkl