import java.io.*;
import java.util.*;

//----------------------------------------------------------

// implements S...

class Dog {
	
	static String[] Names = {"Fido", "Lady", "Rover", "Spike" };
	String name;
	
	
	// assign random name
	
	Dog()
	{
		name = Names[(int)(Math.random()*Names.length)];
	}
	
	
	public String toString()
	{
		return name;
	}
}

//----------------------------------------------------------

public class FileTest {
	
	public static void main(String[] args) {
	
		ArrayList<Dog> Dogs = new ArrayList<Dog>();
		
		for (int i = 0; i < 10; i++)
			Dogs.add(new Dog());

		System.out.println("before saving...");
		System.out.println(Dogs);
		
		System.exit(1);
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		// save and restore the objects
		
		try {
			
			FileOutputStream oFileStream = new FileOutputStream("save.txt");
			ObjectOutputStream os = new ObjectOutputStream(oFileStream);
			
			// first save how many there are...

			os.writeObject(new Integer(Dogs.size()));
			
			// then save the dogs themselves...

			for (Dog d : Dogs)
				os.writeObject(d);
					
			os.close();
			
			// erase Dogs completely

			Dogs.clear();
			System.out.println("see?  no dogs...");
			System.out.println(Dogs);
			
			System.exit(1);
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			// now restore
			
			FileInputStream iFileStream	= new FileInputStream("save.txt");
			ObjectInputStream is = new ObjectInputStream(iFileStream);
			
			// how many dogs are stored here?
			
			Object num_dogs_obj = is.readObject();
			int num_dogs = ((Integer) num_dogs_obj).intValue();
			System.out.println("About to read " + num_dogs + " dogs");
			
			// read 'em in...
			
			for (int i = 0; i < num_dogs; i++) {
				Dog d = (Dog) is.readObject();
				Dogs.add(d);
			}
			
			System.out.println("presto!  they're back...");
			System.out.println(Dogs);
			
			
		} catch (Exception ex) {
			System.out.println("Exception handler!");
			ex.printStackTrace();
		}
	}
}

//----------------------------------------------------------
//----------------------------------------------------------
