Demonstrate CharArrayWriter. : Char Reader Writer « File Input Output « Java






Demonstrate CharArrayWriter.

  
import java.io.CharArrayWriter;
import java.io.FileWriter;
import java.io.IOException;

class CharArrayWriterDemo {
  public static void main(String args[]) throws IOException {
    CharArrayWriter f = new CharArrayWriter();
    String s = "This should end up in the array";
    char buf[] = new char[s.length()];
    s.getChars(0, s.length(), buf, 0);
    f.write(buf);
    System.out.println(f.toString());

    char c[] = f.toCharArray();
    for (int i = 0; i < c.length; i++) {
      System.out.print(c[i]);
    }

    FileWriter f2 = new FileWriter("test.txt");
    f.writeTo(f2);
    f2.close();
    f.reset();
    for (int i = 0; i < 3; i++)
      f.write('X');
  }
}

   
  








Related examples in the same category

1.Char Array IO
2.uses a pair of CharArrayReaders
3.Safe UTF: 64K serialized size
4.UTF8 Reader