Seralization exists?

Posted on 2000-01-05
Medium Priority
Last Modified: 2010-04-16
I am searching a way to save an instance of a object into the file system, something similar to seralization in C++.

Question by:sivanp
1 Comment

Accepted Solution

Jod earned 200 total points
ID: 2325678
You can do it like this...

How to Write to an ObjectOutputStream

Writing objects to a stream is a straight-forward process. For example, the following gets the current time in milliseconds by constructing a Date object and then serializes that object:

FileOutputStream out = new FileOutputStream("theTime");
ObjectOutputStream s = new ObjectOutputStream(out);
s.writeObject(new Date());

ObjectOutputStream is a processing stream, so it must be constructed on another stream. This code constructs an ObjectOutputStream on a FileOutputStream, thereby serializing the object to a file named theTime. Next, the string Today and a Date object are written to the stream with the writeObject method of ObjectOutputStream.

If an object refers to other objects, then all of the objects that are reachable from the first must be written at the same time so as to maintain the relationships between them. Thus the writeObject method serializes the specified object, traverses its references to other objects recursively, and writes them all.

ObjectOutputStream stream implements the DataOutput interface that defines many methods for writing primitive data types, such as writeInt, writeFloat, or writeUTF. You can use these methods to write primitive data types to an ObjectOutputStream.

The writeObject method throws a NotSerializableException if it's given an object that is not serializable. An object is serializable only if its class implements the Serializable interface.

How to Read from an ObjectOutputStream

Once you've written objects and primitive data types to a stream, you'll likely want to read them out again and reconstruct the objects. This is also straight-forward. Here's code that reads in the String and the Date object that was written to the file named theTime in the last example:

FileInputStream in = new FileInputStream("theTime");
ObjectInputStream s = new ObjectInputStream(in);
String today = (String)s.readObject();
Date date = (Date)s.readObject();

Like ObjectOutputStream, ObjectInputStream must be constructed on another stream. In this example, the objects were archived in a file, so the code constructs an ObjectInputStream on a FileInputStream. Next, the code uses ObjectInputStream's readObject method to read the String and the Date objects from the file. The objects must be read from the stream in the same order in which they were written. Note that the return value from readObject is an object that is cast to and assigned to a specific type.
The readObject method deserializes the next object in the stream and traverses its references to other objects recursively to deserialize all objects that are reachable from it. In this way, it maintains the relationships between the objects.

ObjectInputStream stream implements the DataInput interface that defines methods for reading primitive data types. The methods in DataInput parallel those defined in DataOutput for writing primitive data types. They include methods such as readInt, readFloat, and readUTF. Use these methods to read primitive data types from an ObjectInputStream.


Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month5 days, 23 hours left to enroll

588 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question