Full width home advertisement

Java

Tech News

Ad

  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package pkgApplet;
  7.  
  8. import java.applet.Applet;
  9. import java.awt.Button;
  10. import java.awt.GridLayout;
  11. import java.awt.Label;
  12. import java.awt.TextArea;
  13. import java.awt.TextField;
  14. import java.awt.event.ActionEvent;
  15. import java.awt.event.ActionListener;
  16. import java.io.FileInputStream;
  17. import java.io.FileOutputStream;
  18. import java.io.IOException;
  19.  
  20. /**
  21.  *
  22.  * @author smit
  23.  */
  24. public class PR28 extends Applet implements ActionListener {
  25.  
  26. Button b1, b2;
  27. TextField tf;
  28. TextArea ta;
  29. Label l1, l2;
  30.  
  31.  
  32. public void init() {
  33. b1 = new Button("READ");
  34. b2 = new Button("WRITE");
  35. tf = new TextField();
  36. l1 = new Label("ENTER TEXT");
  37. l2 = new Label("Data");
  38. ta = new TextArea(10, 30);
  39. setLayout(new GridLayout(3, 1));
  40.  
  41. add(l1);
  42. add(tf);
  43. add(b1);
  44. add(b2);
  45. add(l2);
  46. add(ta);
  47.  
  48. b1.addActionListener(this);
  49. b2.addActionListener(this);
  50. }
  51.  
  52.  
  53. public void actionPerformed(ActionEvent ae) {
  54. if (ae.getSource() == b1) {
  55. try {
  56. ta.setText(null);
  57. FileInputStream fi = new FileInputStream("D:/Hello.txt");
  58. int i;
  59.  
  60. while ((i = fi.read()) != -1) {
  61. ta.append("" + (char) i);
  62. }
  63. fi.close();
  64. } catch (IOException e) {
  65. System.out.println(e);
  66. }
  67. }
  68. if (ae.getSource() == b2) {
  69. try {
  70. FileOutputStream fo = new FileOutputStream("D:/Hello.txt");
  71. String s = tf.getText();
  72. byte b[] = s.getBytes();
  73. fo.write(b);
  74. fo.close();
  75. } catch (IOException ex) {
  76. System.out.println(ex);
  77. }
  78. }
  79. }
  80. }
  81.  

No comments:

Post a Comment

Bottom Ad [Post Page]