Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Help! java!!

Options
  • 08-03-2005 3:06pm
    #1
    Registered Users Posts: 1,052 ✭✭✭


    I'm trying to write a simple program to read/write to a text file!

    I have it working so it will write to it but can get the reading back in part working!

    Any help would be great!

    import java.io.*;

    public class Bookshop implements Serializable
    {
    private String strTitle;
    private String strAuthor;
    private String strPrice;

    Bookshop(String strTitleIn, String strAuthorIn, String strPriceIn)
    {
    //Assign property values
    strTitle = strTitleIn;
    strAuthor = strAuthorIn;
    strPrice = strPriceIn;
    }

    public String getTitle()
    {
    //Retrieve title
    return strTitle;
    }

    public String getAuthor()
    {
    //Retrieve author
    return strPrice;
    }

    public String getPrice()
    {
    //Retrieve price
    return strPrice;
    }
    }


    AND THIS IS THE BOOKSHOPAPP
    its the end that is all scrwed up! i have it all commented out!

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

    public class BookshopApp extends Frame implements ActionListener
    {
    //Declare stream objects
    FileOutputStream outputBook; //Stream to create file
    ObjectOutputStream objSaveBook; //Stream to save an object
    FileInputStream inputBook;
    ObjectInputStream objGetBook;

    //Declare components
    TextField txtTitle = new TextField(50);
    TextField txtAuthor = new TextField(20);
    TextField txtPrice = new TextField(7);
    Button btnSave = new Button("Save");
    Button btnDisplay = new Button("Display");

    public static void main(String args[])
    {
    //Declare an instance of this application
    BookshopApp thisApp = new BookshopApp();
    thisApp.openInStream();
    thisApp.openOutStream();
    thisApp.createInterface();

    }

    public void openInStream()
    {
    try
    {
    //Create file and object output streams
    outputBook = new FileOutputStream("Book.txt");
    objSaveBook = new ObjectOutputStream(outputBook);
    }
    catch(Exception error)
    {
    System.err.println("Error opening file");
    }
    }

    public void closeInStream()
    {
    try
    {

    objSaveBook.close(); //Close the object output stream
    outputBook.close(); //Close the file output stream
    }
    catch (IOException error)
    {
    System.err.println("Error closing file");
    }
    }

    public void openOutStream()
    {
    try
    {
    //Create file and object input streams
    inputBook = new FileInputStream("Book.txt");
    objGetBook = new ObjectInputStream(inputBook);
    }
    catch(Exception error)
    {
    System.err.println("Error opening file");
    }
    }

    public void closeOutStream()
    {
    try
    {
    objGetBook.close(); //Close the object input stream
    inputBook.close(); //Close the file input stream
    }
    catch (IOException error)
    {
    System.err.println("Error closing file");
    }
    }

    public void createInterface()
    {
    //Set up user interface for this frame
    setTitle("Philip O'Reilly's Bookshop");
    addWindowListener(new WindowAdapter()
    {
    public void windowClosing(WindowEvent event)
    {
    closeOutStream();
    System.exit(0);
    }
    });
    setLayout(new FlowLayout());

    add(new Label("Title "));
    add(txtTitle);
    txtTitle.requestFocus();

    add(new Label("Author "));
    add(txtAuthor);

    add(new Label("Price "));
    add(txtPrice);

    add(btnSave);
    add(btnDisplay);
    btnSave.addActionListener(this);
    btnDisplay.addActionListener(this);

    txtTitle.addActionListener(this);
    txtAuthor.addActionListener(this);
    txtPrice.addActionListener(this);
    setSize(400, 300);
    setVisible(true);
    }

    public void actionPerformed(ActionEvent event)
    {
    try
    {
    //Save Book object
    Bookshop bookCurrent = new Bookshop(txtTitle.getText(), txtAuthor.getText(), txtPrice.getText());

    objSaveBook.writeObject(bookCurrent);

    objSaveBook.flush();
    txtTitle.setText("");
    txtAuthor.setText("");
    txtPrice.setText("");
    txtTitle.requestFocus();
    }
    catch(Exception error)
    {
    System.err.println("Error writing to file");
    //try
    //{
    //Retrieve Book object
    //Bookshop bookCurrent;
    //bookCurrent = (Bookshop) objGetBook.readObject();
    //txtTitle.setText(bookCurrent.getTitle());
    //txtAuthor.setText(bookCurrent.getAuthor());
    //txtPrice.setText(bookCurrent.getPrice());
    //txtTitle.requestFocus();
    //}
    //catch(EOFException eof)
    //{
    //End of file
    //txtTitle.setText("End of File");
    //txtAuthor.setText("");
    //txtPrice.setText("");
    //}
    //catch(Exception error)
    //{
    //System.err.println("Error reading file");
    }
    }
    }


Advertisement