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.

Java

  • 05-03-2005 01:55PM
    #1
    Closed Accounts Posts: 22


    hi ive the following code to save data to a txt file. but its not saving right, can any1 spot what im doing wrong? grid2 is the button i click and the numfld are just textboxs from which i want to take the values entered by user and save them to bookstore.txt.


    // save to text file here
    if(e.getSource() == grid2Btn)

    {
    FileOutputStream outputBookStore;
    ObjectOutputStream objSaveBookStore;
    try


    {

    outputBookStore = new FileOutputStream("BookStore.txt", true);
    objSaveBookStore = new ObjectOutputStream(outputBookStore);
    BookList BkCurrent = new BookList(num1Fld.getText(), num2Fld.getText(), num4Fld.getText(), num5Fld.getText());
    objSaveBookStore.writeObject(BkCurrent);
    objSaveBookStore.flush();
    num1Fld.setText("");
    num2Fld.setText("");
    num4Fld.setText("");
    num5Fld.setText("");

    JOptionPane.showMessageDialog(null, "New Book Details Saved");
    ButtonPanel.add(grid2Btn);
    outputBookStore.close();
    objSaveBookStore.close();
    }
    catch(Exception error)
    {
    System.err.println("Error Opening File");
    }

    }


Comments

  • Registered Users, Registered Users 2 Posts: 6,342 ✭✭✭OfflerCrocGod


    What do you mean "not saving right"? You do realise that you are saving Objects here not textual data so if you open it up in notepad you will get garbage and not human readable text. Explain in greater detail the problems you are having, is that when you try and read in the object using an ObjectInputStream it trows an exception?


  • Closed Accounts Posts: 22 darren85


    yeah i know, but had a tutor go over my code last day and she said its my saving code thats not working but she couldn't see why, for reading it back in the code i have is,

    if(e.getSource() == grid3Btn)
    {
    FileInputStream inputBookList;
    ObjectInputStream ObjGetBookList;
    try

    {

    num1Fld.setEnabled(false);
    num2Fld.setEnabled(false);
    num4Fld.setEnabled(false);
    num5Fld.setEnabled(false);
    grid2Btn.setEnabled(false);


    //Declare stream objects

    inputBookList = new FileInputStream("BookStore.txt");
    ObjGetBookList = new ObjectInputStream(inputBookList);



    System.out.println("a");
    while (inputBookList != null)
    {
    System.out.print("b");

    for(int i=0; i<5; i++)
    {
    BookList BkCurrent;
    BkCurrent = (BookList) ObjGetBookList.readObject();
    //ObjGetBookList.readObject(BkCurrent);
    num1Fld.setText(BkCurrent.getBookName());
    System.out.println(BkCurrent.getBookName());

    //close file and object input streams
    inputBookList.close();
    ObjGetBookList.close();
    }
    }
    }


  • Registered Users, Registered Users 2 Posts: 6,342 ✭✭✭OfflerCrocGod


    darren85 wrote:
    inputBookList.close();
    ObjGetBookList.close();
    }
    }
    }
    Move those two lines outside the for loop, once you close it you wont be able to open it again other then that - which shouldn't stop you from reading at least one value - I can't see a huge problem, it should be OK. Care to tell me why your tutor came to the conclusion that it was your saving code? By the way you are trying something but you are not catching anything.....


Advertisement