//ObjectFile.java //read and write File of objects. "serialization" of an object to a file. //can be mix of different classes and primitives too. //all-in-one example file containing a dummy class X import java.io.*; //import javax.swing.*; public class ObjectFile { public static void main( String[] args ) { X x1 = new X(); //X is defined below X x2 = new X(5,2.3,"hello"); //X[] xArray = new X[100]; //skip the array example for now X x3=null, x4=null; //will get values from file. need to assign something here to fake out compiler... //to see what these X objects look like: System.out.println("x1= " + x1 ); //toString() of X class called System.out.println("x2= " + x2 ); //Write the objects to a file. File outFile; FileOutputStream outFileStream; ObjectOutputStream outObjectStream; //*** try { outFile = new File( "sampleObjects.data" ); outFileStream = new FileOutputStream( outFile ); outObjectStream = new ObjectOutputStream( outFileStream ); outObjectStream.writeObject( x1 ); //*** output ("serialize") object to file outObjectStream.writeObject( x2 ); // array (or Collection) of objects can be dumped to file: //optionally preceded by int number of objects that follow //outObjectStream.writeInt( xArray.length ); //outObjectStream.writeObject( xArray ); outObjectStream.close(); } catch (IOException e) { System.out.println( "threw an IOException...deal with it..." ); } //probably will be "invisible" as the bytes of this data don't happen //to be visible ASCII characters, most likely System.out.println("look at the sampleObjects.data file."); //Read in the objects from the same file into other X objects. File inFile; FileInputStream inFileStream; ObjectInputStream inObjectStream; //*** try { inFile = new File( "sampleObjects.data" ); inFileStream = new FileInputStream( inFile ); inObjectStream = new ObjectInputStream( inFileStream ); //*** x3 = (X) inObjectStream.readObject(); //readObject returns Object x4 = (X) inObjectStream.readObject(); //contents of file can be read into array of objects //optionally preceded by reading size of array //int size = inObjectStream.readInt(); //System.out.println("size= " + size ); //xArray = (X[]) inObjectStream.readObject(); inObjectStream.close(); } catch (IOException e) { System.out.println( "threw an IOException...deal with it..." ); } catch (ClassNotFoundException e) { //readObject can throw this System.out.println( "threw an ClassNotFoundException...deal with it..." ); } //demo purposes to show input from file has happened System.out.println(); System.out.println("x3= " + x3 ); System.out.println("x4= " + x4 ); } } //***class that can read/written from/to file must implement Serializable //***Serializable is an interface that has no methods (i.e. a "marker" interface). //Note that there is nothing here in the class about file input or output. class X implements Serializable { int iX; //this class is of no use. illustration purposes only. double dX; String sX; public X() { //no-arg constructor iX = 0; dX = 0; sX = "zilch"; } public X( int i, double d, String s ) { //constructor iX = i; dX = d; sX = new String( s ); } public String toString() { return "[ " + iX + " " + dX + " " + sX + " ]"; } }