Java I/O How to - Write to file using a BufferedWriter








Question

We would like to know how to write to file using a BufferedWriter.

Answer

//ww  w. j  a v  a 2 s .  co  m
import java.io.BufferedWriter;
import java.io.FileWriter;

public class Main {
  public static void main(String[] args) throws Exception {
    BufferedWriter bufferedWriter = null;
    bufferedWriter = new BufferedWriter(new FileWriter("yourFile.txt"));
    bufferedWriter.write("string");
    bufferedWriter.newLine();
    bufferedWriter.write("string");
  }
}