// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.io.*;

public class BinFile {
   private static void store(DataOutputStream dos, int iv, double xv, String sv) throws IOException {
      dos.writeInt(iv);
      dos.writeDouble(xv);
      dos.writeByte(sv.length());
      dos.write(sv.getBytes());
   }
   private static void load(DataInputStream dis) throws IOException {
      int iv = dis.readInt();
      double xv = dis.readDouble();
      int len = dis.readUnsignedByte();
      byte[] b = new byte[len];
      dis.read(b);
      String sv = new String(b);
      System.out.println(iv + " " + xv + " " + sv);
   }
   public static void main(String[] args) throws IOException {
      DataOutputStream dos = new DataOutputStream(new FileOutputStream("C:\\test.bin"));
      store(dos, 123, 123.456, "ABC");
      store(dos, -1, -2.5, "123");
      dos.close();
      DataInputStream dis = new DataInputStream(new FileInputStream("C:\\test.bin"));
      while(dis.available() > 0) {
         load(dis);
      }
      dis.close();
   }
}
