/* Copyright (c) SEMM NL All rights reserved.
Author : Paul Hamaker. Part of JavaLessons.com
This code is for educational purposes only. Use at own risk.*/

1. import java.awt.* ; 

2. import java.applet.* ; 

3. import java.awt.event.*; 

4. import java.io.*; 

5.  

6. public class ObjectStream 

7.     extends Applet 

8.     implements ActionListener 

9.

10.   FileOutputStream fos = null

11.   ObjectOutputStream oos = null

12.   FileInputStream fis = null

13.   ObjectInputStream ois = null

14.  

15.   TextArea ta ; 

16.   Button btns, btnr ; 

17.  

18.   public void actionPerformed( ActionEvent ev ) 

19.   { 

20.     if ( ev.getSource() == btns ) 

21.     { 

22.       try

23.       fos = new FileOutputStream ( "temptest.obj" ) ; 

24.       oos = new ObjectOutputStream ( fos ) ; 

25.  

26.       Client cl = 

27.       new Client ( 3, "Skipoulos", 45000 ); 

28.  

29.       oos.writeObject( cl ); 

30.  

31.       cl = 

32.       new Client ( 4, "Pizzaiolo", 36750 ); 

33.  

34.       oos.writeObject( cl ); 

35.       } 

36.       catch ( Exception e ) 

37.       { 

38.         ta.append ("== IO Error ===" + e + "\n" ) ; 

39.       } 

40.       finally 

41.       { 

42.       if ( oos != null

43.       try

44.         oos.close (); 

45.       } 

46.       catch ( Exception e ) 

47.       { 

48.         ta.append ("== Error ===" + e + "\n" ) ; 

49.       } 

50.       }///end finally 

51.       return

52.     } 

53.  

54.     if ( ev.getSource() == btnr ) 

55.     { 

56.       try

57.       fis = new FileInputStream ( "temptest.obj" ) ; 

58.       ois = new ObjectInputStream ( fis ) ; 

59.  

60.       Object ob =  ois.readObject(); 

61.  

62.       while ( ob != null

63.       { 

64.         Client cl = ( Client )ob ; 

65.         ta.append ( cl.id + "\n" ) ; 

66.         ta.append ( cl.nm + "\n" ) ; 

67.         ta.append ( cl.amountSpent + "\n" ) ; 

68.         ta.append ( "===================\n" ) ; 

69.         ob =  ois.readObject(); 

70.       } 

71.       } 

72.       catch ( EOFException e ) 

73.       { 

74.         ta.append ("==End Of File reached==\n\n") ; 

75.       } 

76.       catch ( Exception e ) 

77.       { 

78.         ta.append ("== Error ===" + e + "\n") ; 

79.       } 

80.       try 

81.       { 

82.       if ( ois != null

83.         ois.close (); 

84.       } 

85.       catch (Exception e) 

86.       { 

87.         ta.append ( "Error : can't close "  + e + "\n" ) ; 

88.       } 

89.     } 

90.   } 

91.  

92.   public ObjectStream ( ) 

93.   { 

94.     ta = new TextArea ( 12, 40 ) ; 

95.     btns = new Button ( "Save it" ) ; 

96.     btnr = new Button ( "Retrieve it" ) ; 

97.   } 

98.  

99.   public void init() 

100.   { 

101.     add ( btns ) ; 

102.     add ( btnr ) ; 

103.     btns.addActionListener ( this ) ; 

104.     btnr.addActionListener ( this ) ; 

105.     add ( ta ) ; 

106.     setBackground ( Color.yellow ) ; 

107.     validate() ; 

108.   } 

109.

110. //=========== Client.java ================== 

111. class Client 

112.   implements Serializable 

113.

114.  int id ; 

115.  String nm ; 

116.  int amountSpent ; 

117.  

118.  Client ( int pid, String pnm, int pamsp ) 

119.  { 

120.    id=pid;   nm=pnm;   amountSpent=pamsp ; 

121.  } 

122.