import java.awt.*; 
import java.awt.event.*; 
import java.io.*; 

public class MyApplication extends Frame implements ActionListener, WindowListener 
{ 
  static MyApplication myapp; 
  static Label label; 
  static TextArea text; 
  static Button save, load; 

  public static void main(String[] args) 
  { 
    myapp = new MyApplication(); 
    myapp.setLayout(new BorderLayout()); 
    myapp.setSize(500, 400); 
    label = new Label(); 
    myapp.add(label, BorderLayout.NORTH); 
    text = new TextArea(); 
    myapp.add(text, BorderLayout.CENTER); 
    Panel panel = new Panel(new GridLayout(1, 2)); 
    myapp.add(panel, BorderLayout.SOUTH); 
    save = new Button("Text speichern"); 
    save.addActionListener(myapp); 
    panel.add(save); 
    load = new Button("Text laden"); 
    load.addActionListener(myapp); 
    panel.add(load); 
    myapp.addWindowListener(myapp); 
    myapp.setVisible(true); 
  } 

  public void actionPerformed(ActionEvent ev) 
  { 
    try 
    { 
      if (ev.getSource() == save) 
      { 
        FileWriter file = new FileWriter("brief.txt"); 
        String message = text.getText(); 
        file.write(message, 0, message.length()); 
        file.close(); 
        label.setText("Datei wurde erfolgreich gespeichert."); 
      } 
      else 
      { 
        BufferedReader file = new BufferedReader(new FileReader("brief.txt")); 
        StringBuffer message = new StringBuffer(); 
        char c; 
        while (file.ready()) 
        { 
          c = (char)file.read(); 
          message.append(c); 
        } 
        file.close(); 
        text.setText(message.toString()); 
        label.setText("Datei wurde erfolgreich geladen."); 
      } 
    } 
    catch (IOException ex) 
    { 
      label.setText("Fehler: " + ex.getMessage()); 
    } 
  } 

  public void windowClosing(WindowEvent ev) 
  { 
    myapp.setVisible(false); 
    myapp.dispose(); 
    System.exit(1); 
  } 

  public void windowActivated(WindowEvent ev) { } 
  public void windowClosed(WindowEvent ev) { } 
  public void windowDeactivated(WindowEvent ev) { } 
  public void windowDeiconified(WindowEvent ev) { } 
  public void windowIconified(WindowEvent ev) { } 
  public void windowOpened(WindowEvent ev) { } 
} 
