This discussion is archived
4 Replies Latest reply: Aug 17, 2010 1:53 PM by MuhammadKhojaye RSS

Serialization vs java.io

843790 Newbie
Currently Being Moderated
Hi,

I understand that serialization is used to save the objects state in a file. But how is serialization helpful as we can achieve the same thing using the java.io package?

Thanks
  • 1. Re: Serialization vs java.io
    843790 Newbie
    Currently Being Moderated
    Its not serialization vs. I/O, because serialization makes use of java.io to actually store its data to a destination.

    Serialization is useful because it provides a very easy way to persist an object graph if you don't care too much about the storage format.

    Of course you can do the same thing manually, but you'd be writing a lot more code for storing and loading the data. You'd have more control over the format, but that control comes at a price.
  • 2. Re: Serialization vs java.io
    843790 Newbie
    Currently Being Moderated
    Thanks for your reply..

    Can you please tell me what are the different ways to save an object's state other than using serialization?

    Thanks
  • 3. Re: Serialization vs java.io
    843790 Newbie
    Currently Being Moderated
    Neha_Khands wrote:
    Can you please tell me what are the different ways to save an object's state other than using serialization?
    There are many, many ways. Here are some of them:

    - You can use XMLEncoder to write XML in a generic format
    - You can use JAXB to write out a custom, specific XML format
    - You can use Serialization to write a generic binary format (which you can influence by implement the Externalizable interface, making that a partially custom format).
    - You can write out your data manually in a custom binary format using a DataOutputStream
    - You can write out your data manually in a custom text format using Writers

    It's always a trade-off between:

    - amount of code to write
    - ease of manipulation of the data
    - human readability of the output
    - size of the output file
    - ...
  • 4. Re: Serialization vs java.io
    MuhammadKhojaye Newbie
    Currently Being Moderated
    Neha_Khands wrote:
    Can you please tell me what are the different ways to save an object's state other than using serialization?
    See Also XStream.