//******************************************************************** // WriteCountryInfo.java Author: Lewis/Loftus // // Demonstrates object serialization. //******************************************************************** import java.io.*; public class WriteCountryInfo { //----------------------------------------------------------------- // Creates several objects, prints them to standard output, and /// serializes them to a file. //----------------------------------------------------------------- public static void main (String[] args) throws IOException { FileOutputStream file = new FileOutputStream ("countries.dat"); ObjectOutputStream outStream = new ObjectOutputStream (file); CountryInfo[] countries = new CountryInfo[5]; countries[0] = new CountryInfo ("United States of America", "USA", "Washington, D.C.", 9629091L, 278058900L); countries[1] = new CountryInfo ("Russia", "RUS", "Moscow", 17075200L, 145470200L); countries[2] = new CountryInfo ("Italy", "ITA", "Rome", 301230L, 57679800L); countries[3] = new CountryInfo ("Sweden", "SWE", "Stockholm", 449964L, 8875100L); countries[4] = new CountryInfo ("Poland", "POL", "Warsaw", 312685L, 38633900L); int scan; // Print the objects for (scan = 0; scan < countries.length; scan++) System.out.println (countries[scan]); // Serialize the objects to a file for (scan = 0; scan < countries.length; scan++) outStream.writeObject (countries[scan]); } }