FileDocCategorySizeDatePackage
TestSerializable.javaAPI DocExample1920Sun Feb 24 19:28:24 GMT 2002None

TestSerializable

public class TestSerializable extends Object
author
Administrator
version

Fields Summary
private PersonSerializable
p1
private PersonSerializable
p2
Constructors Summary
public TestSerializable()
Creates new TestSerializable

    
        
      
        p1.setDetails("Fred Bloggs",22,'m","\u6B22\u8FCE\u4F7F\u7528\u0020");
        p2.setDetails("Amy Hussain",25,'f","Nuclear Physicist");
    
Methods Summary
public voiddisplayAllData()

        p1.displayDetails();
        p2.displayDetails();
    
public voidloadAllData(java.io.ObjectInputStream ois)

        try {
        p1 = (PersonSerializable) ois.readObject();
        p2 = (PersonSerializable) ois.readObject();
        }
        catch(ClassNotFoundException c) {
            System.out.println("Cannot find class "+c);
        }
    
public static voidmain(java.lang.String[] args)

        
        TestSerializable test = new TestSerializable();
        // Create an binary output stream "test.dat"
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(
                               new File("c:\\test.dat")));
        // Save the two person objects
        test.saveAllData(oos);
        // Close the file
        oos.close();
        // Open a binary intput stream "test.dat"
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(
                                new File("c:\\test.dat")));
        // Retrieve the two person objects
        test.loadAllData(ois);
        // Close the input stream
        ois.close();
        // display the two person objects.
        test.displayAllData();
    
public voidsaveAllData(java.io.ObjectOutputStream oos)

        
        oos.writeObject(p1);
        oos.writeObject(p2);