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

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.       PrintWriter pw = null

19.       try

20.       FileWriter fw = 

21.         new FileWriter ( "temptest.lns" ) ; 

22.  

23.       pw = new PrintWriter ( fw ) ; 

24.  

25.       pw.println ( "String saved on disk" ) ; 

26.       pw.println ( "This 2nd line, too" ) ; 

27.  

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

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

30.       } 

31.       catch ( IOException e ) 

32.       { 

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

34.       } 

35.       finally 

36.       { 

37.       if ( pw != null

38.         pw.close (); 

39.       } 

40.       return

41.     } 

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

43.     { 

44.       BufferedReader br = null

45.       try

46.       FileReader fr = 

47.         new FileReader ( "temptest.lns" ) ; 

48.  

49.       br = new BufferedReader ( fr ) ; 

50.  

51.       ta.append ( "Contents of file temptest.lns :\n\n" ) ; 

52.  

53.       String line = br.readLine ( ) ; 

54.       while ( line != null )     //not EndOfFile 

55.       { 

56.           ta.append ( line + "\n" ) ; 

57.           line = br.readLine ( ) ; 

58.       } 

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

60.       } 

61.       catch ( IOException e ) 

62.       { 

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

64.       } 

65.       try 

66.       { 

67.       if ( br != null

68.         br.close (); 

69.       } 

70.       catch (IOException e) 

71.       { 

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

73.       } 

74.       return

75.     } 

76.   } 

77.  

78.   public StringFiles ( ) 

79.   { 

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

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

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

83.   } 

84.  

85.   public void init() 

86.   { 

87.     add ( btns ) ; 

88.     add ( btnr ) ; 

89.     btns.addActionListener ( this ) ; 

90.     btnr.addActionListener ( this ) ; 

91.     add ( ta ) ; 

92.     setBackground ( Color.orange ) ; 

93.     validate() ; 

94.   } 

95.

96. ==================================== 

97. "D:\\work\\bloyp\\sumfyle" 

98. ======= 

99.     fw = 

100.       new FileWriter ( "temptest.lns",  true  ) ; 

101.