Java IO Tutorial - Java DataOutputStream.writeInt(int v)








Syntax

DataOutputStream.writeInt(int v) has the following syntax.

public final void writeInt(int v)   throws IOException

Example

In the following code shows how to use DataOutputStream.writeInt(int v) method.

/*from w  w  w . jav  a 2s.  c om*/

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class Main {
  public static void main(String[] args) throws IOException {

    int[] i = { 123, 234, 454, 567, 789 };
    FileOutputStream fos = new FileOutputStream("c:\\test.txt");
    DataOutputStream dos = new DataOutputStream(fos);

    for (int j : i) {
      dos.writeInt(j);
    }
    dos.flush();
    InputStream is = new FileInputStream("c:\\test.txt");

    DataInputStream dis = new DataInputStream(is);
    while (dis.available() > 0) {
      int k = dis.readInt();
      System.out.print(k);
    }
  }
}