Charles Bell
Posts: 519
Nickname: charles
Registered: Feb, 2002
|
|
Re: File Writing and Reading
|
Posted: Jun 29, 2002 11:58 AM
|
|
import java.awt.*; import java.awt.event.*; import java.io.*;
import javax.swing.*;
public class JTextFieldTest extends JFrame implements ActionListener{ private JTextField textField1; private JTextField textField2; private JTextField textField3; private JTextField textField4; private String fileName = "UserData.txt"; public JTextFieldTest(){ super("JTextFieldTest"); init(); } public static void main(String[] args){ JTextFieldTest test = new JTextFieldTest(); } public void init(){ textField1 = new JTextField(); textField2 = new JTextField(); textField3 = new JTextField(); textField4 = new JTextField(); JPanel textFieldPanel = new JPanel(); textFieldPanel.setLayout(new GridLayout(4,2)); textFieldPanel.add(new JLabel("Name: ",SwingConstants.RIGHT)); textFieldPanel.add(textField1); textFieldPanel.add(new JLabel("Address: ",SwingConstants.RIGHT)); textFieldPanel.add(textField2); textFieldPanel.add(new JLabel("City: ",SwingConstants.RIGHT)); textFieldPanel.add(textField3); textFieldPanel.add(new JLabel("State: ",SwingConstants.RIGHT)); textFieldPanel.add(textField4); getContentPane().add(textFieldPanel,BorderLayout.NORTH);
JPanel buttonPanel = new JPanel(); buttonPanel.setLayout(new FlowLayout()); JButton saveButton = new JButton("Save"); saveButton.addActionListener(this); buttonPanel.add(saveButton);
JButton loadButton = new JButton("Load"); loadButton.addActionListener(this); buttonPanel.add(loadButton);
JButton clearButton = new JButton("Clear"); clearButton.addActionListener(this); buttonPanel.add(clearButton);
JButton exitButton = new JButton("Exit"); exitButton.addActionListener(this); buttonPanel.add(exitButton);
getContentPane().add(buttonPanel,BorderLayout.CENTER); pack(); setLocationRelativeTo(null); show(); } public void actionPerformed(ActionEvent ae){ String actionCommand = ae.getActionCommand(); if (actionCommand.compareTo("Save") == 0) { save(); }else if (actionCommand.compareTo("Load") == 0) { load(); }else if (actionCommand.compareTo("Clear") == 0) { clear(); }else if (actionCommand.compareTo("Exit") == 0) { exit(); } } public void save(){ try{ FileWriter fw = new FileWriter(fileName); fw.write(textField1.getText() + "\n"); fw.write(textField2.getText() + "\n"); fw.write(textField3.getText() + "\n"); fw.write(textField4.getText() + "\n"); fw.close(); }catch(FileNotFoundException fnfe){ System.err.println("FileNotFoundException: " + fnfe.getMessage()); }catch(IOException ioe){ System.err.println("IOException: " + ioe.getMessage()); } } public void load(){ try{ FileReader fr = new FileReader(fileName); BufferedReader br = new BufferedReader(fr); textField1.setText(br.readLine()); textField2.setText(br.readLine()); textField3.setText(br.readLine()); textField4.setText(br.readLine()); fr.close(); }catch(FileNotFoundException fnfe){ System.err.println("FileNotFoundException: " + fnfe.getMessage()); }catch(IOException ioe){ System.err.println("IOException: " + ioe.getMessage()); } } public void clear(){ textField1.setText(""); textField2.setText(""); textField3.setText(""); textField4.setText(""); } public void exit(){ System.exit(0); } }
|
|