Java Serialization import java.io.*; public class SerializeDemo { public static void main(String [] args) {
//Serialization = The process of converting an object into a byte stream. // Persists (saves the state) the object after program exits // This byte stream can be saved as a file or sent over a network // Byte stream can be saved as a file (.ser) which is platform independent // (Think of this as if you're saving a file with the object's information)
// Steps to Serialize // --------------------------------------------------------------- // 1. Your class should implement Serializable interface // 2. add import java.io.Serializable; // 3. FileOutputStream fileOut = new FileOutputStream(file path) // 4. ObjectOutputStream out = new ObjectOutputStream(fileOut); // 5. out.writeObject(objectName) // 6. out.close(); fileOut.close(); // ---------------------------------------------------------------
//Deserialization = The reverse process of converting a byte stream into an object. // (Think of this as if you're loading a saved file)
// Steps to Deserialize // --------------------------------------------------------------- // 1. Your class should implement Serializable interface // 2. add import java.io.Serializable; // 3. FileInputStream fileIn = new FileInputStream(file path); // 4. ObjectInputStream in = new ObjectInputStream(fileIn); // 5. objectNam = (Class) in.readObject(); // 6. in.close(); fileIn.close(); // ---------------------------------------------------------------
// important notes 1. children classes of a parent class that implements Serializable will do so as well // 2. static fields are not serialized (they belong to the class, not an individual object) // 3. Fields declared as "transient" aren't serialized, they're ignored // 4. the class's definition ("class file") itself is not recorded, cast it as the object type // 5. serialVersionUID is a unique version ID
//SerialVersionUID = serialVersionUID is a unique ID that functions like a version # // verifies that the sender and receiver of a serialized object, // have loaded classes for that object that are compatible // Ensures object will be compatible between machines // Number must match. otherwise this will cause a InvalidClassException // A SerialVersionUID will be calculated based on class properties, members, etc. // A serializable class can declare its own serialVersionUID explicitly (recommended)
Bro i get error in deserialization.. In main class it says java.Lang.ClassNotFoundException:serializer.user I checked everything but i couldn't find out why i get this error.. Pls help bro 😓😭
@@joshuafrancisriin8078 Ensure that the classes for both the serializer and the desializer are organized in the same structure. E.g. if your serializer Main and User classes are in a package say org.example ensure you have the same package org.example for your deserializer Main and User classes. Secondly, contents of the User classes have to be identical.
I have a Software Engineer interview in 2 days, and I never knew what Serializing and Deserializing actually meant in Java, until now. THANKS A TON Bro!
This is the only channel on UA-cam I literally feel I must comment on every time I watch , else I'm a bad guy XD Your videos are great ... Seriously !!
Hey bro, thanks a lot for your amazing videos! I just want to add another detail: if a class has fields that are other custom-made classes, then these classes ALSO have to implement Serializable
Guys, make sure your package file need to be the same in both the serialization folder and deserialization, then you won't get ClassNotFoundExpection for this. And bro you your video is fantastic. Love from China
You're a champ bro! Tried to create a savefile through parsing the objects variables and then reread them and create through it the new objects. This is way easier. Thanks!
Please, please, please make a video on ML or Neural net, please teach how to make a very simple neural net in *JAVA* Even if you suck at those ML things, please figure it out, and make a tutorial on it, pleaseeeeeee
Hey you. If you find this video helpful, pls make sure to like, comment,and subscribe if you want to become a fellow bro. Is that spells right? Because I can speak this sentence in my dream. It’s so familiar.
I am getting java.lang.ClassCastException. It says that both the user classes are in an unnamed module of loader 'app'. What does it mean? A solution to my problem would be great. Thanks.
Very helpful video, thanks. If I knew about this, i wouldn't have to create .txt files using damn FileWriter and split them using _ and . When I wanted to read them, I split the entire long character string using split() method, which resulted in countless problems and headaches, as you can imagine lol.
hey, thanks for the tutorial , however I have a little problem , whenever I use ObjectOutPutStream or InputStream I get an exception wether it was out.writeObject or in.readObject I imported everything and I made sure the error was from those statments when I surrounded them with a try statment please help
I had a problem when I was doing this in VS Code. The software makes packages for both serialization and deserialization folder in the code, which if removed I couldn't run in the VS Code. But even after following Bro's code step by step, I faced the error :- Exception in thread "main" java.lang.ClassCastException: class broCode.Serialization.User cannot be cast to class broCode.Deserialization.User (broCode.Serialization.User and broCode.Deserialization.User are in unnamed module of loader 'app'). This was very annoying cause I followed step by step. So Stack said that I can change the medium of file I'm saving on to something like a JSON file. But if it can work for bro, it can for us. Thus did it without VS Code the manual way by notepad and cmd prompt (also removed the part of packages, even tho both the code lay in different folder) it worked. Typing all of this for anyone who faces Class Cast Exception.
Hi @vishalrai, I guess your problem can be cause you're using different names for package and class names on 2 projects. It should be the same on 2 projects. You can solve this issue by that way. If you don't want they same the name or package. You can use InputStream instead of FileInputStream as below : InputStream is = new FileInputStream("your path file here..."); ObjectInputStream ois = new ObjectInputStream(is); You can solve this issue.
Getting the Classcastexception in my Deserialization package... IDE says everything is correct tho it is not printing the values.. any solutions? Please🥺
Is there anyone here have this problem? "Exception in thread "main" java.lang.NullPointerException: Cannot invoke "Object.getClass()" because "user" is null" When I try to separate serialize and deserialize into 2 projects like in the video, serialize project works fine but deserialize project gives this error. And when I put serialize and deserialize in the same file, it works perfectly. If you know how to fix this problem, please help me!
what is the difference if i store data into json file farmat? i found another youtuber said that serialization is a process to convert data from RAM into string format like json, so that it can be stored and send to your friends, etc. Is he correct?
Hi Sachwini Kumar Khatri; 1.Check if the name of the 2 packages are the same; 2.Check if the name of the 2 class are the same; The both has to be the same;
@@leekbiel If I have all the classes in the same package, it won't let me name them the same thing (2 User classes and 2 Main classes)... What should I do?
@@mahirahman3838 You try to invoke a class that is not inside the classpath. This is a very common issue. Since you did not provide much information thats the only thing I can tell you.
@@Simon-yf7fo I serialized the objects In a project named 'Serialization' but when I created an different project(Deserialization) and try to read those serialize objects ,, that time ClassNotFoundException occured.. Though I did exactly same as bro code.
@@mahirahman3838 In this case I suspect that your filepath to the serialized Object is wrong (.ser file). Otherwise it could have sonething to do with your run configurations.
Java Serialization
import java.io.*;
public class SerializeDemo {
public static void main(String [] args) {
//Serialization = The process of converting an object into a byte stream.
// Persists (saves the state) the object after program exits
// This byte stream can be saved as a file or sent over a network
// Byte stream can be saved as a file (.ser) which is platform independent
// (Think of this as if you're saving a file with the object's information)
// Steps to Serialize
// ---------------------------------------------------------------
// 1. Your class should implement Serializable interface
// 2. add import java.io.Serializable;
// 3. FileOutputStream fileOut = new FileOutputStream(file path)
// 4. ObjectOutputStream out = new ObjectOutputStream(fileOut);
// 5. out.writeObject(objectName)
// 6. out.close(); fileOut.close();
// ---------------------------------------------------------------
//Deserialization = The reverse process of converting a byte stream into an object.
// (Think of this as if you're loading a saved file)
// Steps to Deserialize
// ---------------------------------------------------------------
// 1. Your class should implement Serializable interface
// 2. add import java.io.Serializable;
// 3. FileInputStream fileIn = new FileInputStream(file path);
// 4. ObjectInputStream in = new ObjectInputStream(fileIn);
// 5. objectNam = (Class) in.readObject();
// 6. in.close(); fileIn.close();
// ---------------------------------------------------------------
// important notes 1. children classes of a parent class that implements Serializable will do so as well
// 2. static fields are not serialized (they belong to the class, not an individual object)
// 3. Fields declared as "transient" aren't serialized, they're ignored
// 4. the class's definition ("class file") itself is not recorded, cast it as the object type
// 5. serialVersionUID is a unique version ID
//SerialVersionUID = serialVersionUID is a unique ID that functions like a version #
// verifies that the sender and receiver of a serialized object,
// have loaded classes for that object that are compatible
// Ensures object will be compatible between machines
// Number must match. otherwise this will cause a InvalidClassException
// A SerialVersionUID will be calculated based on class properties, members, etc.
// A serializable class can declare its own serialVersionUID explicitly (recommended)
User user = new User();
user.name = "Bro";
user.password = "I
I think you can even teach me rocket science & Quantum physics with your teaching skills ❤️
Bro i get error in deserialization.. In main class it says
java.Lang.ClassNotFoundException:serializer.user
I checked everything but i couldn't find out why i get this error..
Pls help bro 😓😭
Thanks 🤝
@@joshuafrancisriin8078 Ensure that the classes for both the serializer and the desializer are organized in the same structure.
E.g. if your serializer Main and User classes are in a package say org.example ensure you have the same package org.example for your deserializer Main and User classes. Secondly, contents of the User classes have to be identical.
That is a hard working bro :)
By far, Bro Code has the most succinct, and clearest explanations of Java on UA-cam! No more than needed, no less than required. Good job!
such an amazing tutorial! I was having some trouble working with SQL databases and had to resort to serialization for my cs project, saved my life!
I have a Software Engineer interview in 2 days, and I never knew what Serializing and Deserializing actually meant in Java, until now. THANKS A TON Bro!
Get the job?
@@connorwhitedik yeah, got three interviews, and two offers :)
This is the only channel on UA-cam I literally feel I must comment on every time I watch , else I'm a bad guy XD
Your videos are great ... Seriously !!
This is the guy who needs support! Thanks this tutorial was the best Bro!
You explain it better than my teacher bro. Thank you.
Hey bro, thanks a lot for your amazing videos!
I just want to add another detail: if a class has fields that are other custom-made classes, then these classes ALSO have to implement Serializable
Guys, make sure your package file need to be the same in both the serialization folder and deserialization, then you won't get ClassNotFoundExpection for this. And bro you your video is fantastic. Love from China
thank god for your comment!!!!
I was looking for this for 30min
Great tutorial series! I'm enjoying it
Legend. Solo carrying My Butt through advanced programming
I feel that in my soul
Bro, this is getting serioulized
💕
Clear how-to and break down, still technical but not boring! Thanks Bro Code!
wanted to learn this for a save function, tysm bro
NOTE
Converting an object into a byte stream = serialization
Converting a byte stream into an object = deserialization
Your videos are very very helpful but its been a while that you did not post anything about Java, your community is waiting and thank you for that
Absolute the best serialization tutorial video I have ever watched! Thank you
Best explanation I ever had for this topic! Thank you! Can't believe this is on youtube!
Came back to rewatch the video and brush up on that topic. Thanks, Bro!
You're a champ bro! Tried to create a savefile through parsing the objects variables and then reread them and create through it the new objects. This is way easier. Thanks!
Please, please, please make a video on ML or Neural net, please teach how to make a very simple neural net in *JAVA*
Even if you suck at those ML things, please figure it out, and make a tutorial on it, pleaseeeeeee
Thank you so much for these videos!
Top video. Very informative, and easy to follow as usual.
Thanks Bro.
Hey you. If you find this video helpful, pls make sure to like, comment,and subscribe if you want to become a fellow bro. Is that spells right? Because I can speak this sentence in my dream. It’s so familiar.
Very complicated, good job
Good work bro
Nice
Great job bro:)
Thanks Bro!.. for the new video!
I really want to get started into coding, but don’t know where to start! Could I get any pointers?
🤔Hmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
TYVM Bro code! You are superb!
Dope tutorial! Helped me for an exam!
thanks for this amazing video !
thankz bro amazing way to teaching
keep it up !!!
Awesomely great
Nice!
Thanks Bro!
Great
Helpful video, thank you.
good job
great video thank you 👍
13:42 (Part 2)
Bro, I need a teacher like him in my classes
The perfect video for the topic!
very detail and easy to understand
great stuff, way more complete than what the teacher explained in class lmao
Love you, bro
Thanks Bro.
Great video. All doubts clear.
Need series on Android app development bro !
Great video as the rest you've made.
Another great video as always
13:12
"To something secure such as *password123* "
Lol
Nice tutorial
Thanks mate
great
U re legend thanks so much 🙏
I am getting java.lang.ClassCastException. It says that both the user classes are in an unnamed module of loader 'app'. What does it mean? A solution to my problem would be great. Thanks.
Hi JustSwift_ ;
1.Check if the name of the 2 packages are the same;
2.Check if the name of the 2 class are the same;
The both has to be the same;
@@leekbiel I had already solved the issue. But thanks a lot as those are something to keep in mind.
@@leekbiel Thanks alot man : )
BRO I LOVE YOU
bro doesnt link git but pastes Code into comments. 👌💪
84th. Thank you, ma Bro Sensei
good tutorial
Tremendo!
thank you bro
you are the besst!
thankyouuuu
Thanks
I love you bro
Very helpful video, thanks. If I knew about this, i wouldn't have to create .txt files using damn FileWriter and split them using _ and . When I wanted to read them, I split the entire long character string using split() method, which resulted in countless problems and headaches, as you can imagine lol.
thanks bro
hey, thanks for the tutorial , however I have a little problem , whenever I use ObjectOutPutStream or InputStream I get an exception wether it was out.writeObject or in.readObject I imported everything and I made sure the error was from those statments when I surrounded them with a try statment please help
"ObjectOutPutStream" output is one word so put is not capitalized
so its ObjectOutputStream*
@@Lukas-qy2on thanks, however this wasn't the problem, the class I was serializing didnt implement java.serializable thanks anyway
@@cinemacat4108 same problem! How did you solve it?
@@joshuafrancisriin8078 I forgot to implement Serializable in the class that needs to be saved
I had a problem when I was doing this in VS Code. The software makes packages for both serialization and deserialization folder in the code, which if removed I couldn't run in the VS Code. But even after following Bro's code step by step, I faced the error :- Exception in thread "main" java.lang.ClassCastException: class broCode.Serialization.User cannot be cast to class broCode.Deserialization.User (broCode.Serialization.User and broCode.Deserialization.User are in unnamed module of loader 'app'). This was very annoying cause I followed step by step. So Stack said that I can change the medium of file I'm saving on to something like a JSON file. But if it can work for bro, it can for us. Thus did it without VS Code the manual way by notepad and cmd prompt (also removed the part of packages, even tho both the code lay in different folder) it worked. Typing all of this for anyone who faces Class Cast Exception.
Hi @vishalrai,
I guess your problem can be cause you're using different names for package and class names on 2 projects. It should be the same on 2 projects. You can solve this issue by that way.
If you don't want they same the name or package. You can use InputStream instead of FileInputStream as below :
InputStream is = new FileInputStream("your path file here...");
ObjectInputStream ois = new ObjectInputStream(is);
You can solve this issue.
when i try to make deserialization i got this message( class libraryBook cannot be cast to class java.util.ArrayList)
حرفيا ابونا
Getting the Classcastexception in my Deserialization package... IDE says everything is correct tho it is not printing the values.. any solutions? Please🥺
Hi Aditya Kumar;
1.Check if the name of the 2 packages are the same;
2.Check if the name of the 2 class are the same;
The both has to be the same;
@@leekbiel thanks I was searching for it since hours
beast 🫶
Oh no how do you know my password?:D Thanks for the video, helps so much with Java test prep:)
nc one
liked
commented
subscribed
when u get no thumbs down, u can be sure u did good
hello Bro code Iam your Subscriber can you please upload a video on Annotations because of iam confusing about it
keep on getting ClassNotFoundException error.
saved my life
*insert The prophecy is true meme clip*
Is there anyone here have this problem?
"Exception in thread "main" java.lang.NullPointerException: Cannot invoke "Object.getClass()" because "user" is null"
When I try to separate serialize and deserialize into 2 projects like in the video, serialize project works fine but deserialize project gives this error. And when I put serialize and deserialize in the same file, it works perfectly. If you know how to fix this problem, please help me!
Is it possible to save the file as ".txt" instead of ".ser"?
Yes it is but there is no point in doing that
yes it is I just did that
@@frosty4513 obviosly it is possible but there is no difference in renaming file endings
what is the difference if i store data into json file farmat?
i found another youtuber said that serialization is a process to convert data from RAM into string format like json, so that it can be stored and send to your friends, etc. Is he correct?
json isnt exactly the same. Json is more like an Object, that every Programming Language can interpret and is more readable for humans
ywaah!
why I am getting error = >Exception in thread "main" java.lang.ClassCastException
I have the same problem
I am having the same problem as well. Did you get any solution? If so, please let me know what it is. It would be really helpful.
Hi Sachwini Kumar Khatri;
1.Check if the name of the 2 packages are the same;
2.Check if the name of the 2 class are the same;
The both has to be the same;
@@leekbiel If I have all the classes in the same package, it won't let me name them the same thing (2 User classes and 2 Main classes)... What should I do?
"something secure like... password123" 😆😆
bro code
Where do you work Bro ?
doesn't work :(
If you‘d elaborate I might help
@@Simon-yf7fo I got class not found exception
@@mahirahman3838 You try to invoke a class that is not inside the classpath. This is a very common issue. Since you did not provide much information thats the only thing I can tell you.
@@Simon-yf7fo I serialized the objects In a project named 'Serialization' but when I created an different project(Deserialization) and try to read those serialize objects ,, that time ClassNotFoundException occured.. Though I did exactly same as bro code.
@@mahirahman3838 In this case I suspect that your filepath to the serialized Object is wrong (.ser file). Otherwise it could have sonething to do with your run configurations.
source code please !!!
BRO
statistics
: )
Comment
13:10 update the password to something secure 😂😂
Thanks so much!