Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Persisting multiple objects of the same type to a file.

  1. #1
    Member mjr's Avatar
    Join Date
    Jan 2012
    Location
    127.0.0.1
    Posts
    36
    My Mood
    Fine
    Thanks
    8
    Thanked 2 Times in 1 Post

    Default Persisting multiple objects of the same type to a file.

    Let's say I have a class called "Names", and it has two properties: firstName and lastName.

    And let's say I want to persist the data to a file.

    And let's assume that the first time the firstName is "John" and last name is "Doe". I want to persist that to a file (say, names.txt, for instance).

    But what I'd like to be able to do, also, is persist more names, say for instance "Tom Smith", "Bob Jones", and "Tim Timson".

    Is there a way to do that?

    I've figured out how to persist one object to a file (using serialization), but I can't figure out how to do multiple ones.

    Advice?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,916
    Thanks
    55
    Thanked 2,351 Times in 2,322 Posts

    Default Re: Persisting multiple objects of the same type to a file.

    Have you tried writing out more than one object to a file?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member mjr's Avatar
    Join Date
    Jan 2012
    Location
    127.0.0.1
    Posts
    36
    My Mood
    Fine
    Thanks
    8
    Thanked 2 Times in 1 Post

    Default Re: Persisting multiple objects of the same type to a file.

    Quote Originally Posted by Norm View Post
    Have you tried writing out more than one object to a file?
    Good question. Yes, I have. I neglected to mention that.

    When I try to do that, the only thing that gets written to the file is the final object. I'm assuming what actually happens is that it writes each object, but overwrites that object with the next one.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,916
    Thanks
    55
    Thanked 2,351 Times in 2,322 Posts

    Default Re: Persisting multiple objects of the same type to a file.

    Please post the code that shows the problem.
    How are you writing to the file? Do you close it between writes? Do you specify to append data to the file when opening it for output?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member mjr's Avatar
    Join Date
    Jan 2012
    Location
    127.0.0.1
    Posts
    36
    My Mood
    Fine
    Thanks
    8
    Thanked 2 Times in 1 Post

    Default Re: Persisting multiple objects of the same type to a file.

    Quote Originally Posted by Norm View Post
    Please post the code that shows the problem.
    How are you writing to the file? Do you close it between writes? Do you specify to append data to the file when opening it for output?
    Norm:

    Thanks for asking. Before I post the code, yes, I understand that I shouldn't "hard code" paths to files (i.e. C:\MyFiles\somefile.txt), because of the various file systems on various machines.

    The code below is executed when a button is clicked. I want to be able to change the values, click the button, and have the new value appended to the file. I'll figure out how to read the values back out later.

    Anyway, here's the code.

    btnSave.addActionListener(new ActionListener(){
    			@Override
    			public  void actionPerformed(ActionEvent e){
    				ObjectOutputStream oos = null;
    				try {
    					oos = new ObjectOutputStream(new FileOutputStream("C:\\PersistedData.txt"));
    				} catch (FileNotFoundException e1) {
    					// TODO Auto-generated catch block
    					e1.printStackTrace();
    				} catch (IOException e1) {
    					// TODO Auto-generated catch block
    					e1.printStackTrace();
    				}
    				try {
    					oos.writeObject(new String(lblGeneratedPwd.toString()));
    					oos.writeChars(lblGeneratedPwd.toString());
    				} catch (IOException e1) {
    					// TODO Auto-generated catch block
    					e1.printStackTrace();
    				}
    				try {
    					oos.close();
    				} catch (IOException e1) {
    					// TODO Auto-generated catch block
    					e1.printStackTrace();
    				}
    			}
    		});

    Thanks in advance for your efforts.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,916
    Thanks
    55
    Thanked 2,351 Times in 2,322 Posts

    Default Re: Persisting multiple objects of the same type to a file.

    have the new value appended to the file
    Did you research this question:
    Do you specify to append data to the file when opening it for output?
    Read the API doc for the FileOutputStream class.
    If you don't understand my answer, don't ignore it, ask a question.

  7. The Following User Says Thank You to Norm For This Useful Post:

    mjr (August 15th, 2012)

  8. #7
    Member mjr's Avatar
    Join Date
    Jan 2012
    Location
    127.0.0.1
    Posts
    36
    My Mood
    Fine
    Thanks
    8
    Thanked 2 Times in 1 Post

    Default Re: Persisting multiple objects of the same type to a file.

    Quote Originally Posted by Norm View Post
    Did you research this question:

    Read the API doc for the FileOutputStream class.
    Exactly what I needed. Should have thought about this myself. Thanks headed your way.
    Last edited by mjr; August 15th, 2012 at 08:36 PM.

Similar Threads

  1. Creating multiple objects with a for loop
    By JackCannon15 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 20th, 2011, 07:26 PM
  2. how to get value path file from jsp form- input type file
    By meeGoreng in forum JavaServer Pages: JSP & JSTL
    Replies: 4
    Last Post: October 4th, 2011, 01:05 AM
  3. Creating array from generic type objects?
    By AndrewMiller in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 13th, 2010, 09:22 PM
  4. How to create a pointer type of effect for objects
    By zelalem in forum Object Oriented Programming
    Replies: 6
    Last Post: October 20th, 2010, 03:53 AM
  5. Replies: 6
    Last Post: May 15th, 2009, 06:06 PM