• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

PrintWriter question

 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just ran across the public void print(Object obj) method in the PrintWriter class.  I want to make sure this will do what I think it will.  If I cycle through a Collection of objects and print each to the file, will it write the entire object in a similar fashion to adding it to a collection?  Can I then read each object back into memory without impacting the class structure of the object?
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Paul,
No. It will call toString() on the method and print that. If you want to print the object fields and read them back, look at Serializable and ObjectInputStream
 
Paul Peterson
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you.  I will look into those!
 
Paul Peterson
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I REALLY think your suggestion will do exactly what I am trying to do and found an example online.  I think I have the Serialization part ready to test, but the example only shows working with one object.  The code below is my test project to serialize plus a modified version of the Deserialization from the site.  As I mentioned, the deserialization is only pulling in one object and I need to find a way to take in all of the objects in a collection.

my attempt at serialization




Deserialization -  need to loop or stream through the collection
 
Paul Peterson
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, first problem:
i did something wrong with the serialization.  I got these errors:

java.io.NotSerializableException: com.sg.flooringplayground.TestObject
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1184)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348)
at com.sg.flooringplayground.FlooringMasteryPlayground.testSerialize(FlooringMasteryPlayground.java:185) ->  out.writeObject(t);
at com.sg.flooringplayground.FlooringMasteryPlayground.main(FlooringMasteryPlayground.java:59) -> the call to the method testSerialize(a);
 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Make sure you've added "implements Serializable" to your test object. And that it only contains fields Java knows how to Serialize. (else you'll need to do the same for those objects.)

As far as your list, you can call the input/output methods on the list. It will be smart enough to iterate. Here's a working example:



Note that I'm using Java 7's try with resources to clean up the code a bit. And Java 8 to loop through the list.
 
Paul Peterson
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Forget the error, I forgot to implement Serializable in the TestObject class
 
Paul Peterson
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Getting closer

This is my attempt following your suggestion and the following errors:



Serialized data is saved in temporaryStorage/test.serException in thread "main" java.lang.ClassCastException: com.sg.flooringplayground.TestObject cannot be cast to java.util.ArrayList
at com.sg.flooringplayground.FlooringMasteryPlayground.testDeserialize(FlooringMasteryPlayground.java:204) -> result = (ArrayList<TestObject>) in.readObject();
at com.sg.flooringplayground.FlooringMasteryPlayground.main(FlooringMasteryPlayground.java:60) -> again, this was just the method call
 
Paul Peterson
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have tried several ideas including using the code you suggested with one variation.  The only thing I changed was the print statement to an add to arraylist.  I still get the incompatible cast error above...

Please let me know what I am doing wrong in this code:

 
Paul Peterson
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Eliminated errors, but no usable data yet.

I tried modifying the method to



This will compile and run without errors.  But the return value is not generating anything.  I tried using the following code to display the deserialized info and it doesn't display anything at all.

 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you show your entire application? Both the main you are using to serializable/deserialize and the TestObject?

Note that with the try with resources, you don't need to call close() anymore. It gets closed automatically.
 
Paul Peterson
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The application I was using is just a test bed for various ideas for a larger project.  I pulled the serialize/deserialize code into this file.  It looks like the deserialize code is partially working, but only displaying one object.



and the object class

 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah. In my example code, I serialize the ArrayList object. You looped through and serialized each object individual.

Which is fine. However, you need to use a loop to read the objects back in since you used a loop to write them out. For example:



And yes, ObjectInputStream really does make you loop through until an exception is thrown. Yech.
 
Paul Peterson
Ranch Hand
Posts: 106
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
THANK YOU VERY MUCH!!!

You just made my week!!!

This is the solution I have been looking for for three days!  As I frequently do, I tried to make things much more difficult than they needed to be!

I truly appreciate all of your assistance!!
 
Paul Peterson
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The full context of your last reply is fully sinking through my thick skull.  Are you saying that I can pass the entire array list without looping through each item?  That would simplify things even more.
 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Peterson wrote:The full context of your last reply is fully sinking through my thick skull.  Are you saying that I can pass the entire array list without looping through each item?  That would simplify things even more.


Yes. There's a working example of that in my post that begins with "Make sure you've added "implements Serializable" to your test object"
 
Paul Peterson
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There I go again, making things more difficult than necessary.  That will make this project much easier to complete.

Thank you once again!
 
Story like this gets better after being told a few times. Or maybe it's just a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic