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)
{
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()
{
JFrame frame2 = new JFrame();
panel2 = new JPanel();
textArea = new JTextArea(30,30);
p.add(textArea);
getContentPane().add(p);
p.setVisible(true);
try {
for (int i = 0; i <= size; i++) {
String s = (String)objectInputStream.readObject();
textArea.append(s);
}
}
catch (IOException ioException) {
JOptionPane.showMessageDialog(this, "Error reading file", "Error reading file", JOptionPane.ERROR_MESSAGE);
}
catch (ClassNotFoundException classException) {
JOptionPane.showMessageDialog(this, "Error ", "Error", JOptionPane.ERROR_MESSAGE);
}
}
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);
}
}
}
///////////////////////////////////////////////////////////
//////////////// This is class Book /////////////////////
import java.io.Serializable;
public 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;
}
}
/////////////////////////////////////////////////////////////