Java character read-write class CharArrayReader and Chararraywriter use examples _java

Source: Internet
Author: User
Tags int size

CharArrayReader
CharArrayReader is a character array input stream. It is similar to Bytearrayinputstream, except that Bytearrayinputstream is a byte array input stream, and Chararray is a character array input stream. CharArrayReader is used to read a character array, which inherits from reader. The data for the operation is in 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 APIs in CharArrayReader, refer to Sample 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 Chararrayrea

      Der character stream, content is arrayletters array CharArrayReader car = new CharArrayReader (arrayletters);
          Reads 5 characters for (int i=0; i<len; i++) from a character array stream {//If you can continue to read the next character, read the next character if (car.ready () = = True) {
          Reads the 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 feature, exit if (!car.marksupported ()) {SYSTEM.OUT.PRIntln ("Make not supported!");
      return; }//Mark the next read position in the character stream.
      That is, the tag "F", because 5 characters have been read before, so the next read position is 6th character "//(s)," parameter 0 "in the mark (0) function of the CharArrayReader class is meaningless.

      (), Mark () and reset () are matched, reset () resets the "next read position in the character stream" to the location saved in Mark (0) Car.mark; Skips 5 characters.
      After 5 characters are skipped, the next read value in the character stream should be "K".

      Car.skip (5); Reads 5 data from a character stream.
      Read "Klmno" char[] buf = new Char[len];
      Car.read (buf, 0, LEN);

      System.out.printf ("buf=%s\n", String.valueof (BUF));
      Reset the character stream: that is, reset the next read position in the character flow to where Mark () is marked, that is, F.
      Car.reset (); Reads 5 characters into the BUF from the reset character stream.
      Read "Fghij" Car.read (buf, 0, LEN);
    System.out.printf ("buf=%s\n", String.valueof (BUF));
    catch (IOException e) {e.printstacktrace ();

 }
  }
}

Run Result:

0:a
1:b
2:c
3:d
4:e
Buf=klmno

Chararraywriter
CharArrayReader is used to write the data character, which inherits from writer. The data for the operation is in 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 APIs in Chararraywriter, refer to Sample 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 Chararraywri

      ter character streams chararraywriter caw = new Chararraywriter ();
      Write "a" character caw.write (' a ');
      Writes the string "BC" character Caw.write ("BC");
      System.out.printf ("caw=%s\n", CAW);
      Writes the second 5 characters (DEFGH) in the Arrayletters array from the beginning of "3" to the CAW.
      Caw.write (Arrayletters, 3, 5);

      System.out.printf ("caw=%s\n", CAW); (01) write the character 0//(02) and then write "123456789"//(03) and then write the 第8-12个 character (IJKL) in Arrayletters Caw.appEnd (' 0 '). Append ("123456789"). Append (String.valueof (arrayletters), 8, 12);

      System.out.printf ("caw=%s\n", CAW);
      Compute 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));
      Writes CAW to another output stream chararraywriter caw2 = new Chararraywriter ();
      Caw.writeto (CAW2);
    System.out.printf ("caw2=%s\n", CAW2);
    catch (IOException e) {e.printstacktrace ();

 }
  }
}

Run Result:

CAW=ABCDEFGH0123456789IJKL
size=22
buf=abcdefgh0123456789ijkl
caw2=abcdefgh0123456789ijkl

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.