CS340 Advances in Software Development

squawkpsychoticDéveloppement de logiciels

2 déc. 2013 (il y a 4 années et 4 mois)

85 vue(s)

Advances in Software
Object Oriented Programming in Java
Lecture 12
Hüseyin Akcan

Java I/O Streams

I/O Stream represents an input source or an
output destination

Disk files



Memory arrays



Input stream
Output stream
Streams can
handle all
kinds of data,
from the most
(byte etc) to
the most
(objects etc.)

Character Streams

Grey readers connect to the actual source

White readers do some processing

Byte Streams

Using Streams

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

Character Streams

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
this feature

Character streams (2)

Character streams use byte streams for low
level I/O

Two general purpose byte-to-character bridge
streams available



Use these to create character streams if none

Buffered Streams

Previously mentioned streams perform
unbuffered I/O

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...

Data Streams

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:
Data Type
Item price
Item count
Item description
How can you write these in a single file?
See DataStreamsDemo for more...

Object streams

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

Primitive types

Serializable objects


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
Database connections

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

Recursive objects?

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
comes from
Object 1
Object 2

Custom serialization

You can also customize how an object is

Add private readObject and writeObject
methods to your class

private void writeObject(ObjectOutputStream out)
throws IOException

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...