djlandd
djlandd

Reputation: 11

What is the difference between the object serialization and Filewriter?

I am going through Head first Java,and I have reached Serialization Topic,It states that if If your data will be used by only the Java,program that generated it then use serialization or else If your data will be used by other programs then write to a plain text file.

But i have read that the serialization of an object is JVM independent. Also when we serialize an object,the its data gets saved but when we use the FileWriter and fileReader then what we print gets stored in the file,different from the serialization right?

Also when is a bufferedreader used,how do we use it in cse with filewriter.

Upvotes: 0

Views: 2991

Answers (1)

GhostCat
GhostCat

Reputation: 140417

Serialization is one (of various) concepts that describes how to do achieve persistence for your Java objects. Basically the term refers to a protocol that is used to turn any object into a stream of bits and bytes.

What you do with that stream of bits and bytes very much depends ... on well; what you want do with it.

A FileWriter on the other hand; is a completely unrelated "concept" - basically it is part of the IO framework that Java offers you to resolve tasks like "reading or writing something from/to the file system".

In other words: you can use serialization to turn Java objects into bits and bytes. You can use a FileWriter; or one of its many specific subclasses to get those bits and bytes ... from memory into a file that lives on your disc.

For the other parts of the your question - I wholeheartedly recommend you to study the many many online resources yourself. The intention of this site is to help with programming problems; not to teach you things that you can find in zillions of books or web sites.

To address your comments: if you are dealing with a Java object, you can call its methods. If you are dealing with raw bits and bytes ... basically you can only write those somewhere; or read them from somewhere. And you can take the bytes, and re-constitute a Java object from them. On which you can call methods again. It is like taking your money to your bank account: when you "persist" your money, just the information about the numbers is stored somewhere (but nobody keeps your coins and bills in your account). You can come back later on, the information is looked up; and you receive new coins when doing a withdrawal. Maybe that helps ..

Upvotes: 0

Related Questions