Writes Java objects | Reads Java objects |
Using ArrayList, we can write multiple Java objects | Via ArrayList, we can read and retrieve multiple Java objects |
By default, ArrayList is serializable, and hence no need to implement serializable if we are writing ArrayList of objects like String, but the elements should be serializable. writeObject() is used to write ArrayList of data | During deserialization of ArrayList object, readObject() is used to read the ArrayList of data |
If we are using a class object(like the example as we discussed above), the class should implement serializable and then only while using writeObject() method, java.io.NotSerializableException will not be thrown | We need to cast the same format of object while using readObject() |
writeObject() method writes the serialized data in binary format | readObject() method deserializes and reads the data |
The binary format is not readable. Though we are passing primitive data like String, double, etc., while writing, the data is getting stored in binary format. | In order to get the data in perfect class format, we need to use ObjectInputStream which deserializes data and we can get all primitive data that is written |
As a whole object, it is getting written, there is no loss of data provided the object is serialized and written in a proper format | Similarly, whole object is read, there is no loss of data, and we can easily iterate the data also. |
Not only writing class objects by means writeObject(), it also supports other write methods like as depicted below and many more methods to write for each and every data type.
write(byte[] buffer)-> Writes an array of bytes.
writeBoolean(boolean value) -> Write boolean value
writeDouble(double value)-> Write 64 bit double value
writeInt(int value)-> Writes 32 bit int
|
Not only reading class objects by means readObject(), it also supports other read methods like depicted as below and many more methods to read each and every datatype.
read()-> Reads byte of data
readBoolean() -> To read a boolean.
readDouble()-> To read a 64 bit double.
readInt()->To Read a 32 bit int.
|
Possible exceptions for ObjectOutputStream is as follows:
IOException - in case of any I/O error occurs during writing stream header
SecurityException
NullPointerException - if OutputStream not initialized and hence it may be null
|
Possible exceptions for ObjectInputStream is as follows:
StreamCorruptedException - Incorrect stream header
IOException - In case of any I/O error occurs while reading stream header
SecurityException
NullPointerException - if Inputstream not initialized and hence it may be null
|
Possible exceptions for writeObject is as follows:
InvalidClassException - Serialization of class should be correct and if something is wrong.
NotSerializableException - Some object to be serialized does not implement the java.io.Serializable interface. This will commonly occur and hence it is mandatory to make the class to implement Serializable
IOException - During writing, if IO error occur by the underlying OutputStream.
|
Possible exceptions for readObject is as follows:
InvalidClassException - Serialization of class should be correct and if something is wrong.
ClassNotFoundException - Class of a serialized object should be available and if cannot be found.
IOException - During reading, if IO error occur by the underlying InputStream
StreamCorruptedException
OptionalDataException
|