Java Reference
In-Depth Information
try (ObjectOutputStream objectOut =
new ObjectOutputStream(
new BufferedOutputStream(Files.newOutputStream(file)))){
objectOut.writeObject(myObject);
} catch(IOException e) {
e.printStackTrace();
}
The writeObject() method can throw any of the following exceptions:
InvalidClassException is thrown when there is something wrong with the class definition for
the object being written. This might be because the class is not public , for instance.
NotSerializableException is thrown if the object's class, or the class of a data member of the
class, does not implement the Serializable interface.
IOException is thrown when a file output error occurs.
The first two exception classes here are subclasses of ObjectStreamException , which is itself a subclass
of IOException . Thus, you can catch any of them with a catch block for IOException . Of course, if you
want to take some specific action for any of these then you can catch them individually. Just be sure to put
the catch blocks for the first two types of exception before the one for IOException .
The call to writeObject() takes care of writing everything to the stream that is necessary to reconstitute
the object later in a read operation. This includes information about the class and all its superclasses, includ-
ing their version IDs, as well as the contents and types of the data members of the class. Remarkably, this
works even when the data members are themselves class objects as long as they are objects of classes that
implement the Serializable interface. Our writeObject() call causes the writeObject() method to be
called for each object that is a data member, and this mechanism continues recursively until everything that
makes up our object has been written to the stream. Each independent object that you write to the stream re-
quires a separate call to the writeObject() method, but the objects that are members of an object are taken
care of automatically. This is not completely foolproof in that the relationships between the class objects can
affect this process, but for the most part this is all you need to do.
Writing Primitive Data Types to an Object Stream
You can write data of any of the primitive types using the methods declared in the DataOutput interface and
defined in the ObjectOutputStream class for this purpose. For writing individual items of data of various
types, you have the following methods:
write(int byte)
writeByte(int byte)
writeFloat(float x)
writeDouble(double x)
writeShort(int n)
writeInt(int n)
writeLong(long n)
writeChar(int ch
writeBoolean(boolean val)
The write() and writeByte() methods both write a single byte to the stream. None of the listed meth-
ods returns a value, and they can all throw an IOException because they are output operations.
When you want to write a String object to the file as an object, you normally use the writeObject()
method. You can also write a string to the file as a sequence of bytes using the writeBytes() method by
passing a reference to a String as the argument to the method. In this case, each character in the string is
Search WWH ::




Custom Search