Professional Documents
Culture Documents
Module Objectives
At the end of this module, participants will be
able to:
Define the concept of Streams
Use the various Java IO classes
Read and write to the standard Input and output
streams (Console IO)
Read from and write to files
Differentiate between StringBuffer and
StringBuilders
Illustrate Object Serialization
I/O Streams
The standard java.io package, is defined in terms of streams.
Streams are ordered sequences of data that have a
source (input stream), or
destination (output stream).
Sending data from the application to a data destination is
called writing to a stream.
Retrieving data from a data source is called reading from a
stream.
Streams should be opened for reading, then closed after.
Data
Source
Application
01101101
01101101
01101101
01101101
01101101
Data
Destination
Application
01101101
01101101
01101101
01101101
01101101
Data
Source
Input
Stream
InputStream
Reader
BufferedReader
Application
Console IO
These are the statements for displaying simple text messages to
the user.
System.out.print(...)
System.out.println(...)
File IO
Objects of type File are used to represent the actual files (but
not the data in the files) or directories that exist on a computer's
physical disk.
File thisFile = new File(".\\sef\\module11\\sample\\FileIOSample.java");
Serialization
Serialization allows instances of an object to be represented as
a stream, which can then be written to a data source/destination
The interface Serializable needs to be implemented by any class
whose instances are allowed to be serialized
The field modifier transient is used to mark class members that
should NOT be serialized along with the other state attributes of
the object
11
Serialization (cont.)
To write an object to a stream, use an instance of the
ObjectOutputStream class and chain it to an output stream
destination.
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(target));
out.writeObject(aSerializableObject);
12
Are objects.
Are mutable; once given a value, that value can always change.
All methods in StringBuffer are available in StringBuilder.
StringBuilder class is not thread safe unlike StringBuffer.
13