Binary01.java
Select all
/* Binary01.java David Klick 2005-01-26 Writes and then reads binary data in a file. */ import java.io.*; public class Binary01 { public static void main(String[] args) throws IOException { int n = 72; double x = 1.2, y = 3.4, z = 5.6; String filename = "Binary01.dat"; // write data to file DataOutputStream ofile = new DataOutputStream( new FileOutputStream(filename)); ofile.writeInt(n); ofile.writeDouble(x); ofile.writeDouble(y); ofile.writeDouble(z); ofile.close(); // read data from file DataInputStream ifile = new DataInputStream( new FileInputStream(filename)); System.out.println("Data read from file:"); System.out.println(ifile.readInt()); System.out.println(ifile.readDouble()); System.out.println(ifile.readDouble()); System.out.println(ifile.readDouble()); ifile.close(); } }