Forums Register Login

PrintWriter question

+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
Thank you.  I will look into those!
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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);
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Forget the error, I forgot to implement Serializable in the TestObject class
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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:

+Pie Number of slices to send: Send
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.

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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

+Pie Number of slices to send: Send
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.
1
+Pie Number of slices to send: Send
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!!
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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"
+Pie Number of slices to send: Send
There I go again, making things more difficult than necessary.  That will make this project much easier to complete.

Thank you once again!
ice is for people that are not already cool. Chill with this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 846 times.
Similar Threads
Passed SCEA Part 2 - My experience
trying to use a txt file like a database
A concurrent, thread-safe alternative/complement to collections
Generic Issue
Writing an Empty Line in a File....
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 00:00:32.