Java I/O How to - Write int to a file with buffer








Question

We would like to know how to write int to a file with buffer.

Answer

/*w  w  w  . j av a  2  s.  c om*/
import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class Main {
  public static void main(String[] arguments) {
    try {
      FileOutputStream file = new FileOutputStream("p.dat");
      BufferedOutputStream buff = new BufferedOutputStream(file);
      DataOutputStream data = new DataOutputStream(buff);

      for (int i = 0; i < 400; i++)
        data.writeInt(i);
      data.close();
    } catch (IOException e) {
      System.out.println("Error - " + e.toString());
    }
  }
}