The Java Program: DataOut.java

  1 import java.io.DataOutputStream;
  2 import java.io.FileOutputStream;
  3 
  4 class DataOut {
  5 
  6    public static void main (String[] args) throws Exception {
  7 
  8       if (args.length != 1) {
  9          System.err.println("Usage: java Main <output file>");
 10          System.exit(-1);
 11       }
 12 
 13       FileOutputStream file_out = new FileOutputStream (args[0]);
 14       DataOutputStream data_out = new DataOutputStream (file_out);
 15 
 16       final char a = 'a';
 17       final byte b = 2;
 18       final String c = "abc";
 19       final short d = 4;
 20       final byte[] b2 = {0x41, 0x42, 0x43};
 21 
 22       data_out.write (b);
 23       data_out.write (b2, 0, b2.length);
 24       data_out.writeBoolean (true);
 25       data_out.writeChar (a);
 26       data_out.writeBytes (c);
 27       data_out.writeChars (c);
 28       data_out.writeDouble (123.456);
 29       data_out.writeFloat (123.456f);
 30       data_out.writeInt (678);
 31       data_out.writeLong (678l);
 32       data_out.writeShort (d);
 33       data_out.writeUTF (c);
 34       data_out.writeUTF ("abc\n");
 35       data_out.write (b);
 36       data_out.writeShort (d);
 37       data_out.flush();
 38    
 39       System.out.println ("Size of file written: " + data_out.size());
 40       data_out.close();
 41    }
 42 }