home *** CD-ROM | disk | FTP | other *** search
/ BUG 15 / BUGCD1998_06.ISO / aplic / jbuilder / jsamples.z / Serial2.java < prev    next >
Text File  |  1997-07-30  |  2KB  |  74 lines

  1. // Copyright(c) 1997 ObjectSpace, Inc.
  2.  
  3. import COM.objectspace.jgl.*;
  4. import java.io.*;
  5. import java.util.*;
  6.  
  7. public class Serial2
  8.   {
  9.   static public void write()
  10.     {
  11.     try
  12.       {
  13.       // create a list of names
  14.       DList names = new DList();
  15.       names.add( "Peter Parker" );
  16.       names.add( "Frank Castle" );
  17.       names.add( "Logan" );
  18.       names.add( "Steve Rogers" );
  19.  
  20.       // save names to a file
  21.       ObjectOutput s =  new ObjectOutputStream( new FileOutputStream( "Serial2.bin" ) );
  22.       s.writeObject( names );
  23.  
  24.       // search for some particular entries
  25.       ForwardIterator wolverine = names.find( "Logan" );
  26.       ForwardIterator hulk = names.find( "Bruce Banner" );
  27.  
  28.       // write the iterators to the file as well
  29.       s.writeObject( wolverine );
  30.       s.writeObject( hulk );
  31.       }
  32.     catch ( IOException e )
  33.       {
  34.       System.out.println( "caught: " + e );
  35.       }
  36.     }
  37.  
  38.   static public void read()
  39.     {
  40.     try
  41.       {
  42.       // read sequence and iterator from file
  43.       ObjectInputStream s = new ObjectInputStream( new FileInputStream( "Serial2.bin" ) );
  44.       DList names = (DList)s.readObject();
  45.       ForwardIterator wolverine = (ForwardIterator)s.readObject();
  46.       ForwardIterator hulk = (ForwardIterator)s.readObject();
  47.  
  48.       // check the iterators
  49.       if ( wolverine.equals( names.end() ) )
  50.         System.out.println( "Don't know who Wolverine is" );
  51.       else
  52.         System.out.println( "Wolverine is also known as " + wolverine.get() );
  53.       if ( hulk.equals( names.end() ) )
  54.         System.out.println( "Don't know who the Hulk is" );
  55.       else
  56.         System.out.println( "Hulk is also known as " + hulk.get() );
  57.       }
  58.     catch ( IOException e1 )
  59.       {
  60.       System.out.println( "caught: " + e1 );
  61.       }
  62.     catch ( ClassNotFoundException e2 )
  63.       {
  64.       System.out.println( "caught: " + e2 );
  65.       }
  66.     }
  67.  
  68.   public static void main( String args[] )
  69.     {
  70.     write();
  71.     read();
  72.     }
  73.   }
  74.