// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.awt.*;
import java.awt.event.*;
import java.io.*;

import javax.swing.*;

public class Persist2 extends JFrame implements ActionListener {
    private final static String FILE_NAME = "C:\\p2.dat";
    private JTextField tf1;
    private JTextField tf2;
    private JButton btn;
    public Persist2() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        getContentPane().setLayout(new GridLayout(3, 1));
        tf1 = new JTextField();
        getContentPane().add(tf1);
        tf2 = new JTextField();
        getContentPane().add(tf2);
        btn = new JButton("Save");
        btn.addActionListener(this);
        getContentPane().add(btn);
        pack();
    }
    public void save(String filename) {
        try {
            PrintWriter pw = new PrintWriter(new FileWriter(filename));
            pw.println(tf1.getText());
            pw.println(tf2.getText());
            pw.close();
        } catch (FileNotFoundException ex) {
        } catch (IOException ex) {
        }
    }
    public void load(String filename) {
        try {
            BufferedReader br = new BufferedReader(new FileReader(filename));
            tf1.setText(br.readLine());
            tf2.setText(br.readLine());
            br.close();
        } catch (FileNotFoundException e) {
        } catch (IOException e) {
        }
    }
    public void actionPerformed(ActionEvent e) {
        save(FILE_NAME);
    }
    public static void main(String[] args) {
        Persist2 p2 = new Persist2();
        if((new File(FILE_NAME)).exists()) {
            p2.load(FILE_NAME);
        }
        p2.setVisible(true);
    }
}
