FileDocCategorySizeDatePackage
SerialTest.javaAPI DocExample6517Sun Dec 14 22:47:40 GMT 2003oreilly.hcj.constants

SerialTest

public class SerialTest extends Object
Demonstration of serializing constants.
author
Robert Simmons jr. (kraythe)
version
$Revision: 1.4 $

Fields Summary
Constructors Summary
Methods Summary
public static java.lang.StringgetFilename()
Gets the filename to use as a composition of user home and demo.dat.

return
The constructed filename.

		String filename =
			System.getProperty("user.home") + System.getProperty("file.separator")
			+ "demo.dat";
		return filename;
	
public static final voidmain(java.lang.String[] args)
Main Demo Method.

param
args Command line args.
throws
RuntimeException If there is a problem during the execution.

		try {
			writeCountry4();
			readCountry4();
			System.out.println();
			writeCountry5();
			readCountry5();
			System.out.println();
			writeCountry6();
			readCountry6();
		} catch (final FileNotFoundException ex) {
			throw new RuntimeException(ex);
		} catch (final IOException ex) {
			throw new RuntimeException(ex);
		} catch (final ClassNotFoundException ex) {
			throw new RuntimeException(ex);
		}
	
public static voidreadCountry4()
Reads a Country4 from a file.

throws
FileNotFoundException If the data file is missing.
throws
IOException If there is a problem with the write.
throws
ClassNotFoundException If there is a problem finding country classes used.

		// --
		System.out.println("Country4 in VM");
		System.out.println("Type = " + Country4.GERMANY.getClass());
		System.out.println("Name = " + Country4.GERMANY.getName());
		System.out.println("Hashcode = " + Country4.GERMANY.hashCode());
		// --   
		FileInputStream fis = new FileInputStream(getFilename());
		ObjectInputStream ois = new ObjectInputStream(fis);
		Country4 inCountry = (Country4)ois.readObject();

		// --
		System.out.println("----------------");
		System.out.println("Country4 read in");
		System.out.println("Type = " + inCountry.getClass());
		System.out.println("Name = " + inCountry.getName());
		System.out.println("Hashcode = " + inCountry.hashCode());
		// --   
		System.out.println("----------------");
		System.out.println("Identical = " + (inCountry == Country4.GERMANY));
	
public static voidreadCountry5()
Reads a Country5 from a file.

throws
FileNotFoundException If the data file is missing.
throws
IOException If there is a problem with the write.
throws
ClassNotFoundException If there is a problem finding country classes used.

		// --
		System.out.println("Country5 in VM");
		System.out.println("Type = " + Country5.GERMANY.getClass());
		System.out.println("Name = " + Country5.GERMANY.getName());
		System.out.println("Hashcode = " + Country5.GERMANY.hashCode());
		// --   
		FileInputStream fis = new FileInputStream(getFilename());
		ObjectInputStream ois = new ObjectInputStream(fis);
		Country5 inCountry = (Country5)ois.readObject();

		// --
		System.out.println("----------------");
		System.out.println("Country5 read in");
		System.out.println("Type = " + inCountry.getClass());
		System.out.println("Name = " + inCountry.getName());
		System.out.println("Hashcode = " + inCountry.hashCode());
		// --   
		System.out.println("----------------");
		System.out.println("Identical = " + (inCountry == Country5.GERMANY));
	
public static voidreadCountry6()
Reads a Country6 from a file.

throws
FileNotFoundException If the data file is missing.
throws
IOException If there is a problem with the write.
throws
ClassNotFoundException If there is a problem finding country classes used.

		// --
		System.out.println("Country6 in VM");
		System.out.println("Type = " + Country6.GERMANY.getClass());
		System.out.println("Name = " + Country6.GERMANY.getName());
		System.out.println("Hashcode = " + Country6.GERMANY.hashCode());
		// --   
		FileInputStream fis = new FileInputStream(getFilename());
		ObjectInputStream ois = new ObjectInputStream(fis);
		Country6 inCountry = (Country6)ois.readObject();

		// --
		System.out.println("----------------");
		System.out.println("Country6 read in");
		System.out.println("Type = " + inCountry.getClass());
		System.out.println("Name = " + inCountry.getName());
		System.out.println("Hashcode = " + inCountry.hashCode());
		// --   
		System.out.println("----------------");
		System.out.println("Identical = " + (inCountry == Country6.GERMANY));
	
public static voidwriteCountry4()
Writes an Country4 to a file.

throws
IOException If there is a problem with the write.

		FileOutputStream fos = new FileOutputStream(getFilename());
		ObjectOutputStream oos = new ObjectOutputStream(fos);
		oos.writeObject(Country4.GERMANY);
	
public static voidwriteCountry5()
Writes an Country5 to a file.

throws
IOException If there is a problem with the write.

		FileOutputStream fos = new FileOutputStream(getFilename());
		ObjectOutputStream oos = new ObjectOutputStream(fos);
		oos.writeObject(Country5.GERMANY);
	
public static voidwriteCountry6()
Writes an Country5 to a file.

throws
IOException If there is a problem with the write.

		FileOutputStream fos = new FileOutputStream(getFilename());
		ObjectOutputStream oos = new ObjectOutputStream(fos);
		oos.writeObject(Country6.GERMANY);