Java CharArrayWriter class

Introduction

CharArrayWriter implements an output stream to use an array as the destination.

CharArrayWriter has two constructors, shown here:

CharArrayWriter()  
CharArrayWriter(int numChars) 

The close() method has no effect on a CharArrayWriter.


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

public class Main {
   public static void main(String args[]) throws IOException {
      CharArrayWriter f = new CharArrayWriter();
      String s = "the is a test from demo2s.com";
      char buf[] = new char[s.length()];

      s.getChars(0, s.length(), buf, 0);

      try {//  w  w w.ja  v  a 2 s  .  co m
         f.write(buf);
      } catch (IOException e) {
         System.out.println("Error Writing to Buffer");
         return;
      }

      System.out.println("Buffer as a string");
      System.out.println(f.toString());
      System.out.println("Into array");

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

      System.out.println("\nTo a FileWriter()");

      // Use try-with-resources to manage the file stream.
      try (FileWriter f2 = new FileWriter("test.txt")) {
         f.writeTo(f2);
      } catch (IOException e) {
         System.out.println("I/O Error: " + e);
      }

      System.out.println("Doing a reset");
      f.reset();

      for (int i = 0; i < 3; i++){
         f.write('X');
      }
      System.out.println(f.toString());
   }
}



PreviousNext

Related