Java by API/java.io/CharArrayReader — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 14:32, 31 мая 2010

new CharArrayReader(char[] buf)

 
import java.io.CharArrayReader;
import java.io.CharArrayWriter;
import java.io.IOException;
public class Main {
  public static void main(String args[]) throws IOException {
    CharArrayWriter outStream = new CharArrayWriter();
    String s = "This is a test.";
    for (int i = 0; i < s.length(); ++i)
      outStream.write(s.charAt(i));
    System.out.println("outstream: " + outStream);
    System.out.println("size: " + outStream.size());
    CharArrayReader inStream;
    inStream = new CharArrayReader(outStream.toCharArray());
    int ch = 0;
    StringBuffer sb = new StringBuffer("");
    while ((ch = inStream.read()) != -1)
      sb.append((char) ch);
    s = sb.toString();
    System.out.println(s.length() + " characters were read");
    System.out.println("They are: " + s);
  }
}





new CharArrayReader(char[] buf, int offset, int length)

 
import java.io.CharArrayReader;
import java.io.IOException;
public class Main {
  public static void main(String args[]) throws IOException {
    String tmp = "abcdefghijklmnopqrstuvwxyz";
    int length = tmp.length();
    char c[] = new char[length];
    tmp.getChars(0, length, c, 0);
    CharArrayReader input1 = new CharArrayReader(c);
    CharArrayReader input2 = new CharArrayReader(c, 0, 5);
    int i;
    while ((i = input1.read()) != -1) {
      System.out.print((char) i);
    }
    while ((i = input2.read()) != -1) {
      System.out.print((char) i);
    }
  }
}