Java I/O How to - Create BufferedWriter from FileWriter and write strings to the file








Question

We would like to know how to create BufferedWriter from FileWriter and write strings to the file.

Answer

/*w w  w .j  ava2 s .  c  om*/


  

import java.io.BufferedWriter;
import java.io.FileWriter;

class BufferedWriterDemo {

  public static void main(String args[]) throws Exception {

    FileWriter fw = new FileWriter(args[0]);

    BufferedWriter bw = new BufferedWriter(fw);

    // Write strings to the file
    for (int i = 0; i < 12; i++) {
      bw.write("Line " + i + "\n");
    }

    // Close buffered writer
    bw.close();
  }
}