Advances in Software
Object Oriented Programming in Java
Java I/O Streams
I/O Stream represents an input source or an
kinds of data,
from the most
(byte etc) to
Grey readers connect to the actual source
White readers do some processing
Variety of stream input/output classes are
Use the one appropriate for you and your data
If doing low level I/O is not something you
need, use a derived class of
Java character stream I/O automatically
translates to and from local character set
It has built in support for internationalization
Use character streams instead of byte streams
when working with characters to benefit from
Character streams (2)
Character streams use byte streams for low
Two general purpose byte-to-character bridge
Use these to create character streams if none
Previously mentioned streams perform
Means each read and write is reflected to the
underlying OS immediately
This makes programs much less efficient
Instead Java uses BufferedStreams, which
read/write to a buffer and the changes are
reflected when the buffer is empty/full.
Buffered Streams (2)
Usually you create a buffered stream by using
an unbuffered stream
inputStream = new BufferedReader(newFileReader("inputfile.txt"));
outputStream = new BufferedWriter(new FileWriter("outputFile.txt"));
Available buffered stream classes
See InputTest.java for more...
How can we write and read built in types?
Java has data streams for this!!!
Data streams support binary I/O of primitive
data type values (boolean, char, byte, short, int,
long, float, and double) as well as String values
Data Streams example
Read and write:
How can you write these in a single file?
See DataStreamsDemo for more...
We can send basic types to streams, can we
also do the same with complex objects?
Yes, using the Java object serialization
Object streams (2)
Object has to implement
A markup interface with no methods
If all fields of the class is either
Transient fields are not serialized, or restored during
What to serialize?
You cannot serialize some of the Java objects
Anything which has an active connection, such as
Other than that, most of the Java objects are
What are written into the stream?
Class of the object
Signature of the class
Values of the fields
Be carefull here, if you have references to other
objects, they are written too!!!
Might be a huge stream if your objects have a lot of
references to other huge objects
All reference objects should also be serializable!
See SerializationDemo.java for more...
How is the object written?
When you serialize an object, all the fields that are necessary
to reconstruct that object are also written into the stream
Each object is assigned a serial number
ObjectOutputStream checks the serial number
Will not write the previously written objects
Each object is written once, and this is where
You can also customize how an object is
Add private readObject and writeObject
methods to your class
private void writeObject(ObjectOutputStream out)
private void readObject(ObjectInputStream in)
throws IOException, ClassNotFoundException
Good idea to call out.defaultWriteObject() and
in.defaultReadObject() from these methods
Use case for custom serialization
Assume you have an hashMap in your class, that is
inconcenient to serialize
Might have extra unnecessary fields
Serializing hashMaps are not a good idea, the
implementation can change with JDK version
Each JDK system has a different random generator, so the
order of elements might not be correct
Mark the hashmap transient, and within writeObject
serialize each element separately
When reading create an empty hashmap, and add each
object separately into hashmap
See CustomSerialization.java for more...