Results 1 to 9 of 9
  1. #1
    sanzy is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default saving data b4 quitin

    hi how can i save data in java b4 quittin??
    **************************************
    * 0. Quit *
    * *
    * 1. Add a new Desk *
    * 2. List All Desk *
    * 3. Delete a Desk *
    * 4. Set Desk on Exhibition *
    * 5. Set Desk not on Exhibition *
    * 6. List Desks on Exhibition *
    * 7. List Desks Not on Exhibition *
    * 8. Search By Period *
    * 9. Sort By Period *
    **************************************

    Enter your selection.

    this is the console view of my programe i m stuck on 8th option Search by period , in my prog i have to add a desk every tym (it should save it and no need to add a new desk every tym when i run it)
    i m totally confused ...

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Quote Originally Posted by sanzy View Post
    hi how can i save data in java b4 quittin??
    **************************************
    * 0. Quit *
    * *
    * 1. Add a new Desk *
    * 2. List All Desk *
    * 3. Delete a Desk *
    * 4. Set Desk on Exhibition *
    * 5. Set Desk not on Exhibition *
    * 6. List Desks on Exhibition *
    * 7. List Desks Not on Exhibition *
    * 8. Search By Period *
    * 9. Sort By Period *
    **************************************

    Enter your selection.

    this is the console view of my programe i m stuck on 8th option Search by period , in my prog i have to add a desk every tym (it should save it and no need to add a new desk every tym when i run it)
    i m totally confused ...
    As am I(confused that is) please try to use proper English. Shorthand just makes for confusing posts. Your title suggests you want to save, but im not sure the question has anything to do with saving, or does it? Please explain better.

  3. #3
    sanzy is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default

    thx for your reply here is the specification

    Implement the ‘persistence’ requirements in your system such that the system reads the artefact Model data from
    a file on start-up and saves the current Model data to the file when the user selects ‘Quit’. The user should NOT
    be required to ‘Load’ or ‘Save’ the data manually.
    You should also extend the system to allow sorting and searching of the artefacts by at least 1 ‘field’. This will
    require you to add new methods to the Model class and new menu options to the ConsoleView class.

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You may want to check the tutorial section on saving. Are you using serialized objects or manually writing and reading the file?

  5. #5
    sanzy is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default

    code has no errors but its still not saving data...



    public boolean saveDesk(){
    boolean savedOK = true;
    try {
    XMLEncoder encoder = new XMLEncoder(new FileOutputStream(FILENAME));
    encoder.writeObject(Desks);
    encoder.close();
    } catch (Exception e) {
    savedOK = false;
    }
    return savedOK;
    }


    private void quit() {
    boolean ok = DesksModel.saveDesk();
    if(ok)
    System.out.println("Data saved OK.");
    else
    System.out.println("Warning - problems with saving data.");
    }

  6. #6
    lovelesh is offline Senior Member
    Join Date
    Nov 2010
    Location
    Delhi
    Posts
    135
    Blog Entries
    1
    Rep Power
    0

    Smile

    Is control of your program is going in quit() method?

  7. #7
    sanzy is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default

    Yes
    If u scroll up there is Console view of the programme option 0 is for quitting

  8. #8
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    i'm guessing you are using serialization to write the Desks object?
    when you say it is not saving - do you mean no file is created or the file is blank or looks like gobbledegook when you open it with notepad or something? also what is the string you are feeding into "FILENAME"?

  9. #9
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    Java Code:
        public static void writeDeskList(java.util.List<Desk> deskList) {
            String filename = "FILENAME";
            FileOutputStream fos = null;
            ObjectOutputStream out = null;
            try {
                fos = new FileOutputStream(filename);
                out = new ObjectOutputStream(fos);
                out.writeObject(deskList);
                out.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            System.out.println("Saved desks.");
        }

    the above works fine for me

Similar Threads

  1. saving a string data.
    By imorio in forum New To Java
    Replies: 7
    Last Post: 11-18-2010, 12:33 PM
  2. Hi! Saving data??
    By Gahstanz in forum New To Java
    Replies: 1
    Last Post: 06-29-2010, 07:29 AM
  3. What do you use for saving data from table?
    By ocean in forum New To Java
    Replies: 1
    Last Post: 11-21-2009, 08:08 PM
  4. Saving data...?
    By easyRyder in forum New To Java
    Replies: 8
    Last Post: 07-15-2008, 04:14 AM
  5. Saving data in an XML file
    By Thez in forum New To Java
    Replies: 1
    Last Post: 12-08-2007, 10:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •