Serialization, How Java writes and sends data to files...
Passionate about exploring new technologies and turning ideas into innovative solutions. Currently diving deep into backend development and AI fundamentals, with hands on projects that bridge learning with real world impact. Always curious, always building.
Hey everyone, so I have recently been studying Java and getting more and more into CS fundamentals, and I came across this concept in Java called "Serialization"...
Now what exactly is Serialization? To give you a jist of what it is,
Serialization is a concept in Java through which we can convert the objects in Java to a stream of bytes and send these stream of bytes to a file or save it over a network (HTTP) or send it to kafka etc..
Now why does this concept exist? What is the need of it?
So when we create an object and once its task is done, JVM deletes the object from heap memory, but while programming there are a few objects data that we might need to store it for later lets say storing an object in a database, sharing it over a network etc..
This is where serialization helps us, we can serialize an object store in a file or whatever we want to do it And later when we need the object we can just deserialize it and use it...
Sounds easy, isnt it?? Thats what I thought before i tried implementing Serialization and Deserialization on my own....
Let us try to implement serialization for a Person class having age, password and name…
Later on as I try and explore more into Java and Serialization I will keep updating this blog…
Step 01-
Let us first create a Person class with getter and setter of
Age
Name
Password

As you can see in the image I have first initialised an object of the Person and set its name age password..
Now lets say we want to store the object’s data in a file.txt
Step 02-
So what we will first do is, first we will initialise the file output stream, this stream is used to read and write files in our folder…
Also a point to note is that if the file (ex- file.txt) does not exist the file output stream will create the file automatically for you and perform whatever actions are required..
Step 03-
The next stream we have to use is the Object Output Stream..This is the stream the converts the Person object to stream of bytes…
Now remember that whenver we want Serialize an object we have to make sure that the class of that object implements Serializable as serializable is an interface…
When initialising object output stream we have to pass in the file object where we want to save serialized obect in the file, in our case foc is the file object that we just created earlier so we will pass in that as our parameter method..
Step 04-
and once its done we will use the writeObject method from OOC object to write the Person object data in the file provided in the file output stream
This writeObject is the method from the ObjectOutputStream that converts the object’s data to a series of bytes which is Serialization
And once we have done the object serialization and transfer of data to file, make sure to write
foc.close();
oos.close();
since it is a good coding practise and then once its closed no one make any changes in the data or file…
We have finally serialized the object’s data and written it in our file..we can use Serialization for writing objects data to a file, sharing it over HTTP network or publishing it to Kafka…..
Now once we our serialized done and we store it in our file, we have another concept in Java that is Deserialization to deserialize the serialized object data….
We shall explore Deserialization in another blog…
Thank you if you reached till here, means a lot :)
Always curious, Always building……