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 9 of 9

Thread: Serializable arraylist as txt?

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    22
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Serializable arraylist as txt?

    Hello, I used 'Serializable' on an arraylist, to save as a txt file, how do I make it 'readable' ? right now when I open the file it's just jibberish
    oh! I suck at Java, so help me in english

    Do I have to change something here?

    	        try {
    	            System.out.println("serializing list");
    	            FileOutputStream fout = new FileOutputStream("registrys.ser");
    	            ObjectOutputStream oos = new ObjectOutputStream(fout);
    	            oos.writeObject(registrys);
    	            oos.close();
    	        }
    	        catch (Exception e) {
    	            e.printStackTrace();
    	        }


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Serializable arraylist as txt?

    Are you asking how to deserialize the list? The ObjectInputStream has a readObject() method, which you can cast into whatever the object was. I believe that method deserializes. Not sure if it works for ArrayLists, but you can give it a shot.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Serializable arraylist as txt?

    when I open the file it's just jibberish
    The file contains binary data not text.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Junior Member
    Join Date
    Oct 2013
    Posts
    22
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Serializable arraylist as txt?

    deserialize makes it possible to 'load' a list when you start a program, yes? I will look at that, but not really what I need at this moment.

    Quote Originally Posted by Norm View Post
    The file contains binary data not text.
    yes, exactly, can I make a file containing text?

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Serializable arraylist as txt?

    Yes. You will need to write special code to write out the contents of the ArrayList as readable text.
    What do you want to do with the disk file?
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Oct 2013
    Posts
    22
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Serializable arraylist as txt?

    Quote Originally Posted by Norm View Post
    Yes. You will need to write special code to write out the contents of the ArrayList as readable text.
    What do you want to do with the disk file?
    What do you mean 'disk file'?

    All I want is to make it readable outside eclipse

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Serializable arraylist as txt?

    By "disk file" I meant a file that was written on a disk. I suppose it could also have been written to a memory stick. Basically I meant the file was written to a place out side of the program where it would be kept permanently.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    Oct 2013
    Posts
    22
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Serializable arraylist as txt?

    Quote Originally Posted by Norm View Post
    By "disk file" I meant a file that was written on a disk. I suppose it could also have been written to a memory stick. Basically I meant the file was written to a place out side of the program where it would be kept permanently.
    okey, well it's fine, I'll read some, it's not that urgent, thx anyway!

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Serializable arraylist as txt?

    Another possibility is the XMLEncoder class.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Why POJO class implements Serializable Interface ?
    By noorul11 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 28th, 2013, 07:34 AM
  2. Replies: 0
    Last Post: November 13th, 2012, 07:02 AM
  3. Generating array (or arraylist) matrix from txt file
    By benjalizana in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 14th, 2011, 08:09 PM
  4. Serializable compiler warning
    By kc120us in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: September 25th, 2011, 04:12 PM
  5. what is the use of transient class and serializable interface?
    By chinni in forum Object Oriented Programming
    Replies: 3
    Last Post: October 28th, 2009, 05:48 PM