Java I/O How to - Write short to a file








Question

We would like to know how to write short to a file.

Answer

//from   w  w w .j a  va2 s. com
import java.io.DataOutputStream;
import java.io.FileOutputStream;

public class Main {

  public static void main(String[] args) throws Exception {
    FileOutputStream fos = new FileOutputStream("C:/WriteShort.txt");
    DataOutputStream dos = new DataOutputStream(fos);

    short s = 1;
    dos.writeShort(s);
    dos.close();
  }
}