Java I/O How to - Write to file using Buffered Stream








Question

We would like to know how to write to file using Buffered Stream.

Answer

/*  w  w  w.  j  a v  a  2  s.c  o m*/
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;

public class Main {

  public static void main(String[] args) throws Exception {
    BufferedOutputStream bufferedOutput = new BufferedOutputStream(new FileOutputStream(
        "yourFile.txt"));
    bufferedOutput.write("Line one".getBytes());
    bufferedOutput.write("\n".getBytes());

    bufferedOutput.write(65);
    bufferedOutput.close();
  }
}