Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Help! java!!

  • 08-03-2005 03:06PM
    #1
    Registered Users, Registered Users 2 Posts: 1,056 ✭✭✭


    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