CharArrayReader
CharArrayReader is a character array input stream. It is similar to ByteArrayInputStream, except that ByteArrayInputStream is the byte array input stream, while CharArray is the character array input stream. CharArrayReader is used to read a character array, which is inherited from the Reader. The data of the operation is in units of characters!
CharArrayReader function list:
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)
Sample code:
For detailed usage of API in CharArrayReader, refer to the example code (CharArrayReaderTest.java):
import java.io.CharArrayReader;import java.io.CharArrayWriter;import java.io.IOException;public class CharArrayReaderTest { private static final int LEN = 5; // Corresponding to the English letter "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 test function*/ private static void tesCharArrayReader() { try { // Create a CharArrayReader character stream, the content is the ArrayLetters array CharArrayReader car = new CharArrayReader(ArrayLetters); // Read 5 characters from the character array stream for (int i=0; i<LEN; i++) { // If you can continue to read the next character, read the next character if (car.ready() == true) { // Read "next character of the character stream" char tmp = (char)car.read(); System.out.printf("%d : %c/n", i, tmp); } } // If "this character stream" does not support the tagging function, then exit if (!car.markSupported()) { System.out.println("make not supported!"); return ; } // Mark "the next read position in the character stream". That is, -- mark "f", because 5 characters have been read before, the next read position is the 6th character" // (01), "parameter 0" in the mark(0) function of the CharArrayReader class has no practical significance. // (02), mark() and reset() are matching, reset() will reset "the next read position in the character stream" to "the saved position in the mark()" car.mark(0); // Skip 5 characters. After skipping 5 characters, the next read value in the character stream should be "k". car.skip(5); // Read 5 data from the character stream. That is, read "klmno" char[] buf = new char[LEN]; car.read(buf, 0, LEN); System.out.printf("buf=%s/n", String.valueOf(buf)); // Reset "character stream": that is, reset "the next read position in the character stream" to "the position marked by mark()", that is, f. car.reset(); // Read 5 characters from the "reset character stream" into buf. That is, read "fghij" car.read(buf, 0, LEN); System.out.printf("buf=%s/n", String.valueOf(buf)); } catch (IOException e) { e.printStackTrace(); } }} Running results:
0 : a1 : b2 : c3 : d4 : ebuf=klmnobuf=fghij
CharArrayWriter
CharArrayReader is used to write data characters, which is inherited from Writer. The data of the operation is in units of characters!
CharArrayWriter function list
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)
Sample code:
For detailed usage of API in CharArrayWriter, refer to the example code (CharArrayWriterTest.java):
import java.io.CharArrayReader;import java.io.CharArrayWriter;import java.io.IOException;public class CharArrayWriterTest { private static final int LEN = 5; // Corresponding to the English letter "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 test function*/ private static void tesCharArrayWriter() { try { // Create CharArrayWriter character stream CharArrayWriter caw = new CharArrayWriter(); // Write "A" characters caw.write('A'); // Write "BC" characters caw.write("BC"); //System.out.printf("caw=%s/n", caw); // Write the last 5 characters (defgh) from "3" in the ArrayLetters array to caw. caw.write(ArrayLetters, 3, 5); //System.out.printf("caw=%s/n", caw); // (01) Write character 0 // (02) Then write "123456789" // (03) Then write characters 8-12 in ArrayLetters (ijkl) caw.append('0').append("123456789").append(String.valueOf(ArrayLetters), 8, 12); System.out.printf("caw=%s/n", caw); // Calculate length int size = caw.size(); System.out.printf("size=%s/n", size); // Convert to byte[] array char[] buf = caw.toCharArray(); System.out.printf("buf=%s/n", String.valueOf(buf)); // Write caw to another output stream CharArrayWriter caw2 = new CharArrayWriter(); caw.writeTo(caw2); System.out.printf("caw2=%s/n", caw2); } catch (IOException e) { e.printStackTrace(); } }} Running results:
caw=ABCdefgh0123456789ijklsize=22buf=ABCdefgh0123456789ijklcaw2=ABCdefgh0123456789ijkl