All pastes #288239 Raw Edit

Anonymous

public java v1 · immutable
#288239 ·published 2006-12-22 03:35 UTC
rendered paste body
//////////////// This is class Test /////////////////////////////////////import java.awt.*;import javax.swing.*;import java.io.*;public class Test extends JFrame{         public JTextField dFields[];    public JTextField aFields[];    public Book book;    private ObjectOutputStream objectOutputStream;    private FileOutputStream fos1;            private FileInputStream fis;    private ObjectInputStream objectInputStream;        private JPanel panel2;    private JTextArea textArea;    private JPanel p;    private int size;                public Test(int size)    {    	this.size = size;        dFields = new JTextField[size];        aFields = new JTextField[size];                setVisible(true);        pack();        p = new JPanel();        p.setLayout(new FlowLayout());        Container contentPane = getContentPane();                         for (int i = 0; i<size; i++) {                         dFields[i] = new JTextField(5);            p.add(dFields[i]);                        aFields[i] = new JTextField(5);            p.add(aFields[i]);                        contentPane.add(p);                    }                                                try {            fos1 = new FileOutputStream(f);            objectOutputStream = new ObjectOutputStream(fos1);                        fis = new FileInputStream(f);            objectInputStream = new ObjectInputStream(fis);           }         catch (IOException ioException) {                JOptionPane.showMessageDialog(this, "Error with file.", "Error with file", JOptionPane.ERROR_MESSAGE);                            }    }     File f = new File("test");        public void read()    {           JPanel panel2 = new JPanel();        textArea = new JTextArea(30,30);        panel2.add(textArea);        setContentPane(panel2);               	System.out.println(size);        try {                   for (int i = 0; i < size; i++) {                              String s = (String)objectInputStream.readObject();                              textArea.append(s);			      System.out.println(s);                   }                            }            catch (IOException ioe) {                ioe.printStackTrace();            }            catch (ClassNotFoundException ce) {                ce.printStackTrace();            }    }                public void save()    {            try {                for (int i = 0; i < size; i++) {                       book = new Book(dFields[i].getText(), aFields[i].getText());                    objectOutputStream.writeObject(book.toString());                }            }                    catch (IOException ioe) {                JOptionPane.showMessageDialog(this, "Error saving file.", "Error saving file", JOptionPane.ERROR_MESSAGE);                            }    }            public static void main(String[] args) {    	Test test = new Test(10);	test.setSize(400,400);	test.setVisible(true);		while (test.isVisible()) {		try {			Thread.sleep(10);		} catch (InterruptedException ie) {		}	}			test.save();	test.read();	        test.setVisible(true);	while (test.isVisible()) {		try {			Thread.sleep(10);		} catch (InterruptedException ie) {		}	}	System.exit(0);    }}/////////////////////////////////////////////////////////////////////////// This is class Book /////////////////////class Book implements Serializable{    public String description;    public String amount;    public Book(String description, String amount)    {        this.description = description;        this.amount = amount;    }        public String getAmount()    {        return amount;    }            public String getDescription()    {        return description;    }        public String toString() {        return description + " = " + amount;    }}/////////////////////////////////////////////////////////////