Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialKevin Faust
15,353 Pointssigh lost again as usual...
Hi
so serialization seemed simple enough. we just save the state of the object and we can also load it. hmm probably would only take a few lines. so we create a new class and we have a save method inside it. so far so good. and then BAM OUTTA NOWHERE. ummm......
FileOutputStream fos,
new FileOutputStream("treets.ser"),
ObjectOutputStream oos = new Object
OutputStream(fos), oos.writeObject(treets),
catch(IOException ioe),
ioe.printStackTrace()
and then some more to add to the combo
public static Treet[] load(),
Treet[] treets = new Treet[0];
FileInputStream fis = new FileInputStream("treets.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
treets = (Treet[]) ois.readObject();
catch(IOException ioe)
ioe.printStackTrace();
catch(ClassNotFoundException cnfe)
cnfe.printStackTrace();
and then we add this to the main file:
Treets.save(treets); Treet[] reloadedTreets = Treets.load(); for (Treet reloaded : reloadedTreets) { System.out.println(reloaded); }
and then we add this to the treet.java file:
private boolean mBreakIt = true; ....and then we commented it but then uncommented again....
im completely lost and need a explanation that makes sense and is beginner friendly. there was some repl stuff at the end,some copying/pasting, some commenting/uncommenting, and some other stuff but i have no idea on those either.
5 Answers
Craig Dennis
Treehouse TeacherHi Kevin P !
Sorry about the info dump. I agree that it's a lot to do just to serialize some objects. What I was trying to do here was get us a way to get a hold of the Treets thus far. I introduce file streams, and don't go to deep, because I didn't think it belonged here. I underestimated the thirst for knowledge on this stuff, you aren't the only student who wanted to know how this worked.
The serialization process of objects in Java are versioned. And it is done using that final field and Serializable
interface like we saw. The reason for this is that the class might change and the data won't line up with the newer fields. That's what the serialver
application was about (the commenting/uncommenting), In reality, you probably won't be using file serialization too much, and you will be using a database and an Object Relational Mapper (ORM) to store and retrieve your objects. Some ORMs do use the same versioning, and we'll go over that when we get to it.
I plan to take another swing at this video in the future to make it more clear that I'm just giving you some code to help out, don't worry about understanding it fully.
Sorry for the frustration :(
Kevin Faust
15,353 PointsHey Craig Dennis ,
No need to be sorry. Frustration just sortof comes naturally when learning. When I watch your videos, I feel that I'm supposed to understand every single thing you say. I'm not sure what I'm just supposed to know briefly, what things that i really have to understand to move on, or things that I shouldn't pay much attention for now. When you introduced that huge code, I felt that in order to move on, I have to understand how each line works. Generally when I dont understand, I watch some youtube videos on the topic to confirm my knowledge. But in terms of serialization, there was really nothing online so I felt pretty lost. I tried the challenge after the video however I had no problem on that. Im just not sure what I really have to know from this video as of now.
Craig Dennis
Treehouse TeacherI just watched it again...
I think as long as you know about :
- Using the comma to implement multiple interfaces
- The import *
- The concept of serialization storing an object and loading that object later, either during the same process or a different one
The try-with-resources is handy to understand what is happening in the parens after the try, as you might encounter older code that doesn't use it, but uses the finally block instead to do the cleanup. If you are on Java 7 or higher, you can refactor that code to use try-with-resources.
Hope that helps!
Oshedhe Munasinghe
8,108 PointsHi Craig
Can't you please do a little light simple serialization video? I was also confused and it was really heavy.
Kenneth Black
23,140 PointsI agree that the difficulty spike here is palpable, as someone who is learning java as my third language, this is the first video that I didn't have to up the speed on - while that's nice for me, it doesn't bode well for beginners. It's good to hear that this class is on the list for remakes. As a side note, though, it would be nice to also have a version of these classes that goes at this pace for those of us who might not be new to Programming, but are new to Java. Just food for thought.
Keep up the awesome work, Craig :)
Mridul Gupta
2,144 Pointsok, maybe i can explain it this way,
1) Serializable interface does not have any methods as it is a marker interface and it just marks that the class objects can be serialized and stored in a file.
2)
FileOutputStream fos = new FileOutputStream("treets.ser");
to save an object you need to create a stream to write to a file and this line does that. http://docs.oracle.com/javase/7/docs/api/java/io/FileOutputStream.html
3)
ObjectOutputStream oos = new ObjectOutputStream(fos);
So that we can save those objects of the class which we have serialized. http://docs.oracle.com/javase/7/docs/api/java/io/ObjectOutputStream.html
4)Now finally we need to to do writeObject();
I think you know now what that means.
5) We can understand the same for the InputStream.
PS. I am not an expert, just trying to help. :P Refer to java documentation for more clarity.
Kevin Faust
15,353 Pointsthanks for your answer but i dont understand what you wrote at all and the documentation makes no sense
Grzegorz Gancarczyk
10,064 PointsI'm also lost unfortunatelly, but at the same time I know 'Intermediate' course shouldn't be so easy that you could only watch videos once and understand everything. To be honest, I understand like 30% from this video, but this is not the first time I understand so little.
I can't tell you how it works and this topic isn't new so you probably know it now, but I can tell you how I deal with harder tasks and videos. I just watch them once, then no matter how much I don't understand I go to the next video. After finishing a block of videos I come back to the one I didn't understand and they magically become easy. In later videos things we learn here are used more frequently so you get use to them.
That's how I do it, I'm just trying to get as much as I can from watching it once. After couple of videos I get familiar to these things.
Alper Uyanık
4,749 PointsGrzegorz I will follow your advise, let's see this time the topics will get easier..?
Mridul Gupta
2,144 PointsMridul Gupta
2,144 Pointshttp://javabeginnerstutorial.com/core-java-tutorial/java-serialization-concept-example/
This may help you out. :)