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

8.     extends Applet 

9.     implements ActionListener 

10.

11.   TextArea ta ; 

12.   Button btnupd, btnr ; 

13.   

14.   public void actionPerformed( ActionEvent ev ) 

15.   { 

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

17.     { 

18.       RandomAccessFile raf = null

19.       try

20.       raf = new RandomAccessFile ( "temptest.bin", "rw" ) ; 

21.       raf.readInt ( ) ; 

22.       long pos = raf.getFilePointer(); 

23.       double amt = raf.readDouble ( ) ; 

24.  

25.       amt = amt * 1.1

26.       raf.seek ( pos ) ; 

27.       raf.writeDouble ( amt ) ; 

28.  

29.       ta.append ( "Data has been updated\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 ( raf != null

39.       try

40.         raf.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.       int nr = dis.readInt ( ) ; 

58.       double amt = dis.readDouble ( ) ; 

59.       String s1 = dis.readUTF ( ) ; 

60.       String s2 = dis.readUTF ( ) ; 

61.  

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

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

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

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

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

67.  

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

69.       } 

70.       catch ( Exception e ) 

71.       { 

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

73.       } 

74.       try 

75.       { 

76.       if ( dis != null

77.         dis.close (); 

78.       } 

79.       catch (Exception e) 

80.       { 

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

82.       } 

83.       return

84.     } 

85.   } 

86.  

87.   public UpdateFile ( ) 

88.   { 

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

90.     btnupd = new Button ( "Update" ) ; 

91.     btnr = new Button ( "Retrieve all" ) ; 

92.   } 

93.  

94.   public void init() 

95.   { 

96.     add ( btnupd ) ; 

97.     add ( btnr ) ; 

98.     btnupd.addActionListener ( this ) ; 

99.     btnr.addActionListener ( this ) ; 

100.     add ( ta ) ; 

101.     setBackground ( Color.orange ) ; 

102.     validate() ; 

103.   } 

104.

105.