Java Reference
In-Depth Information
converted to a byte using the default charset. You can write a string simply as a sequence of Unicode charac-
ters by using the writeChars() method, again with a reference of type String as the argument. This writes
each Unicode character in the string as two bytes. Note that the writeBytes() and writeChars() methods
write just a sequence of bytes or characters. No information about the original String object is written so
the fact that these characters belonged to a string is lost.
You have two methods defined in the ObjectOutputStream class that apply to arrays of bytes:
write(byte[] array) writes the contents of array to the file as bytes.
write(byte[] array, int offset, int length) writes length elements from array to the
file starting with array[offset] .
In both cases just bytes are written to the stream as binary data, not the array object itself. An array of
type byte[] is written to the stream as an object by default, so you need to use these methods only if you do
not want an array of type byte[] written as an object.
You can mix writing data of the basic types and class objects to the stream. If you have a mixture of
objects and data items of basic types that you want to store in a file, you can write them all to the same
ObjectOutputStream . You just have to make sure that you read everything back in the sequence and form
in which it was written.
Implementing the Serializable Interface
A necessary condition for objects of a class to be serializable is that the class implements the Serializable
interface, but this may not be sufficient, as you see later. In most instances, you need only declare that the
class implements the Serializable interface to make the objects of that class type serializable and prefer-
ably define the serialVersionUID member that I introduced earlier. If you don't define this member of the
class, a default is created by the serialization runtime, but this is not foolproof and can result in an exception
of type InvalidClassException being thrown during deserialization. No other code is necessary. For ex-
ample, the following declares a class that implements the interface:
public MyClass implements Serializable {
// Definition of the rest of the class...
private static final long serialVersionUID = 9002L;
}
As long as all the fields in MyClass are serializable then simply declaring that the class implements the
Serializable interface and defining serialVersionUID as a final static class member is sufficient to make
objects of type MyClass serializable. If your class is derived from another class that implements the Seri-
alizable interface, then your class also inherits the implementation of Serializable , so you don't have
to declare that this is the case. Let's try this out on a simple class to verify that it really works.
TRY IT OUT: Writing Objects to a File
You first define a serializable class that has some arbitrary fields with different data types:
Search WWH ::




Custom Search