Java DataOutputStream .writeShort (int v)

Syntax

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

public final void writeShort(int v)   throws IOException

Example

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


//from  w w  w. j  a  v a 2  s .co  m
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 {
    short[] s = { 123, 234 };

    FileOutputStream fos = new FileOutputStream("c:\\test.txt");

    DataOutputStream dos = new DataOutputStream(fos);

    for (short j : s) {
      dos.writeShort(j);
    }

    dos.flush();

    InputStream is = new FileInputStream("c:\\test.txt");

    DataInputStream dis = new DataInputStream(is);

    while (dis.available() > 0) {
      short k = dis.readShort();
      System.out.print(k);
    }

  }
}