Binary02.java
Select all
/* Binary02.java David Klick 2005-01-26 Writes and then reads binary data in a file using a class for the record to make life easier. */ import java.io.*; class MyRecord { public int n; public double x, y, z; public void write(DataOutputStream os) throws IOException { os.writeInt(n); os.writeDouble(x); os.writeDouble(y); os.writeDouble(z); } public void read(DataInputStream is) throws IOException { n = is.readInt(); x = is.readDouble(); y = is.readDouble(); z = is.readDouble(); } public String toString() { return "n = " + n + ", x = " + x + ", y = " + y + ", z = " + z; } } public class Binary02 { public static void main(String[] args) throws IOException { MyRecord[] rec = new MyRecord[10]; MyRecord tmp = new MyRecord(); // initialize array of records for (int i=0; i