All pastes #288188 Raw Edit

Untitled

public text v1 · immutable
#288188 ·published 2006-12-22 02:43 UTC
rendered paste body
//////////////// This is class Test /////////////////////////////////////

import java.awt.*;
import javax.swing.*;
import java.io.*;

public class Test extends JFrame
{ 
    private GraphicalUserInterface Uinterface;

    public Book book;

    private ObjectOutputStream objectOutputStream;
    private FileOutputStream fos1;
    
    
    private FileInputStream fis;
    private ObjectInputStream objectInputStream;
    
    private JPanel panel2;
    private JTextArea textArea;
    
    
    
    public Test()
    {
        Uinterface = new GraphicalUserInterface();
        
        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);
                
            }

    } 
    
    public void read()
    {
        Container contentPane = getContentPane();    
        JFrame frame2 = new JFrame();
        panel2 = new JPanel();

        textArea = new JTextArea(30,30);

        frame2.getContentPane().add(panel2);
        panel2.add(new JScrollPane(textArea));       

        frame2.setVisible(true);


        try {
                   for (int i = 0; i <= Uinterface.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);
            }
    }
    
    

    File f = new File("test.txt");

    public void save()
    {
            try {
                for (int count = 0; count < Uinterface.size; count++) {
   
                    book = new Book(Uinterface.dFields[count].getText(), Uinterface.aFields[count].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;
    }
}
/////////////////////////////////////////////////////////////