/* 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.  

7. public class ByteStream 

8.     extends Applet 

9.     implements ActionListener 

10.

11.   TextArea ta ; 

12.   Button btns, btnr ; 

13.  

14.   public void actionPerformed( ActionEvent ev ) 

15.   { 

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

17.     { 

18.       FileOutputStream fos = null

19.       DataOutputStream dos = null

20.       try

21.       fos = new FileOutputStream ( "temptest.bin" ) ; 

22.       dos = new DataOutputStream ( fos ) ; 

23.  

24.       dos.writeInt ( 253 ) ; 

25.       dos.writeDouble ( 450.32 ) ; 

26.       dos.writeUTF ( "String saved as Unicode" ) ; 

27.       dos.writeUTF ( "but efficiently, one byte per ANSI-character" ) ; 

28.  

29.       ta.append ( "Data has been saved on disk\n\n" ) ; 

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

31.       } 

32.       catch ( Exception e ) 

33.       { 

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

35.       } 

36.       finally 

37.       { 

38.       if ( dos != null

39.       try

40.         dos.close (); 

41.       } 

42.       catch ( Exception e ) 

43.       { 

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

45.       } 

46.       }///end finally 

47.       return

48.     } 

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

50.     { 

51.       FileInputStream fis = null

52.       DataInputStream dis = null

53.       try

54.       fis = new FileInputStream ( "temptest.bin" ) ; 

55.       dis = new DataInputStream ( fis ) ; 

56.  

57.       while (true

58.       { 

59.         int nr = dis.readInt ( ) ; 

60.         double amt = dis.readDouble ( ) ; 

61.         String s1 = dis.readUTF ( ) ; 

62.         String s2 = dis.readUTF ( ) ; 

63.  

64.         ta.append ( "Data has been retrieved from disk\n\n" ) ; 

65.         ta.append ( nr + "\n" ) ; 

66.         ta.append ( amt + "\n" ) ; 

67.         ta.append ( s1 + "\n" ) ; 

68.         ta.append ( s2 + "\n" ) ; 

69.  

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

71.       } 

72.       }  

73.       catch ( EOFException e ) 

74.       { 

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

76.       } 

77.       catch ( Exception e ) 

78.       { 

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

80.       } 

81.       try 

82.       { 

83.       if ( dis != null

84.         dis.close (); 

85.       } 

86.       catch (Exception e) 

87.       { 

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

89.       } 

90.       return

91.     }     

92.   } 

93.  

94.   public ByteStream ( ) 

95.   { 

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

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

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

99.   } 

100.  

101.   public void init() 

102.   { 

103.     add ( btns ) ; 

104.     add ( btnr ) ; 

105.     btns.addActionListener ( this ) ; 

106.     btnr.addActionListener ( this ) ; 

107.     add ( ta ) ; 

108.     setBackground ( Color.orange ) ; 

109.     validate() ; 

110.   } 

111.