The character-oriented output streams are all subclasses of class writer, and their class hierarchy is shown in the figure.

The following table lists the main subclasses and descriptions of writers.

Writing files using the filewriter class

The filewriter class is a subclass of the writer subclass outputstreamwriter class,So the filewriter class can use either the methods of the writer class or the methods of the outputstreamwriter class to create objects.

When writing to a file using the filewriter class,You must first call the filewriter () constructor to create an object of the filewriter class,Then call the writer () method. The format of the filewriter constructor is:

 public filewriter (string name);//Create a writable output stream object based on the file name
  public filewriter (string name, boolean a);//a is true, data will be appended to the file

[Example] Use the filewriter class to write ascII characters to a file

import java.io. *;
class ep10_3 {
  public static void main (string args []) {
    try {
      filewriter a=new filewriter ("ep10_3.txt");
      for (int i=32;i<126;i ++) {
        a.write (i);
      a.close ();
    catch (ioexception e) {}

After running the program,Open the ep10_3.txt file, the display content is:

! "#$%&" () * +,-./0123456789:;<=>?@abcdefghijklmnopqrstuvwxyz [\] ^ _ `abcdefghijklmnopqrstuvwxyz {|}

Writing to a file using the bufferedwriter class

The bufferedwriter class is used to write data to a buffer.You must create a filewriter class object when you use it, and then create an object of the bufferedwriter class with this object as a parameter.Finally you need to flush the buffer with the flush () method.The bufferedwriter class has two constructors,Its format is:

 public bufferedwriter (writer out);//Create buffer character output stream
  public bufferedwriter (writer out, int size);//Create an output stream and set the buffer size

[Example] Use the bufferedwriter class for file copying

import java.io. *;
class ep10_4 {
  public static void main (string args []) {
    string str=new string ();
    try {
      bufferedreader in=new
      bufferedreader (new filereader ("ep10_4_a.txt"));
      bufferedwriter out=new
      bufferedwriter (new filewriter ("ep10_4_b.txt"));
      while ((str=in.readline ())!=null) {
        system.out.println (str);
        out.write (str);//Write 1 line of data read to the output stream
        out.newline ();//Write a newline character
      out.flush ();
      in.close ();
      out.close ();
    catch (ioexception e) {
      system.out.println ("An error occurred" + e);

have to be aware of is,When calling the write () method of the out object to write data,No carriage returns are written,Therefore, you need to use the newline () method to add a carriage return after each line of data.To ensure that the target file is consistent with the source file.

  • Previous jQuery plugin Timelinr for timeline effects
  • Next GUI Design Basis for Applications in Java Programming