UNITS [11
asicnpufoutput stream
3.2 Reading and Writing Files
Objectives
1. Differentiate berween binary and textual files.
2, Decide when to use byte and character streams based on the type of daca.
3, Discuss reading and writing to a file using byte and character streams.
Introduction
‘A Java program keeps data in che memory of the JVM (Java Vircual Machine) while
‘executing. This data is inthe form of variables, structures, and arrays that have been
declared by the program. The question is where did this data come from, and what
can the program do with it?
1. Data can come from some location external to the program. Data moved
from an external location into RAM, where the program can access it,
is called input. The keyboard and disk files are the most common sources
of program input.
2. Data can also be sent to a location external to the program; this is called
output. The most common destinations for ourput are the screen, a
printer and disk files.
Input sources and output destinations are collectively referred co as devices. The
keyboard isa device; the screen isa device, and so on. Some devices (the keyboard) are
for input only, others (the screen) are for output only, and still others (disk files) are
for both input and output. Whatever the device, and whether it's performing input
‘or ouput, Java carries out all input and output operations by means of streams.
Understanding of input/output stream in Java
Performing reading and weiting operations on a file can be considered the flow of
data berween two entities. For example, when the JVM is launched and is executinga
Java program, we can imagine the JVM isa black box that is executing the statements
ofa program, Whenever a statement performs a reading operation, it is considered
that there is a flow of daca from the outside world into the black box. Performing
4 writing operation, however, can be considered a flow of data from the black box
to the outside world, With respect to a file, a reading operation transfers the file
contents (data) to the VM, and a writing operation refers to the data flow out of
the JVM and the data are written toa file as its contents. The pattern of dara flow
mentioned above can be considered a stream of data, like the flow of water in a
scream. The flow of water (daca) is sequential and is aimed in a unique direction.12] WAWASAN OPEN UNIVERSITY
E102 Computing
‘There are two fundamental types of data unit. They ate the 8-bit byte and 16-bit
(2-byte) Unicode characters, which correspond to the primitive types byte and char
in Java, [Fan object supports a flow of data in units of byte, itis known as a byte
stream, whereas a character stream supports a flow of data in units of char. Byce
streams and character streams are usually used to handle (byte-oriented) binary files
and (character-based) textual data respectively as shown in Figure 3.3.
B-bit byt Data WE (2-byte) Unice
tay cr!
Figure 3.3 The hierarchy of the data units corresponding to their Input/Output stream
‘According to the directions of the daca flow, they can be further sub-categorised as
an input stream and an outpue stream that correspond 0 the flow of data into the
JVM and the flow of data out of the JVM as shown in Figure 3.4. Input streams
and output streams are usually nicknamed source streams and destination (or sink)
streams respectively
Input operation Output operation
>
Source Destination
Cs =) [a > Cee
Figure 3.4 The input/output operations with respect to the JVM
In the following subsections, we discuss how to apply the idea of dasa streams to
perform file reading and writing operations.
Byte streams
[As the storage units of most storage media or communication channels are natively
byte, byte-based streams are the fundamental way to perform input/output
‘operations. A byte stream is a flow of data in units of byte, and i can be a byte-
based input stream or output stream. In the Java software library, they correspond
to.wo general (more exactly, abstract) classes java, io, Inpatstreamand java
io. OutputStream, They define general types of how an input byce stream and an
output byte stream should behave respectively.UNITS [13
Basicinptoutputstresm
@ Activity 3.2
List some real-world applications thac used byte streams for reading
and writing files
‘The outputstream class
“The abstract superclass java. io.ourput Stream (hereafier OutputStream for
simplicity) defines overloaded write () methods so that the data in units of byee
supplied to che method are written to a particular data destination, depending on
the data destination the object associates with, Afterall data have been written to
the data destination, itis generally preferable ro call the close) method of the
abject to release the resources it acquired, such as the allocated memory, and co
rake sure the data supplied to the write () method callsare properly written to che
data destination, Figure 3.5 visualises the concept behind using an ucputstream
object.
Oueputstream
write) Gweive(b + int) Data
>| serose() F destination
Figure 3.5 The scenario of writing a byte to a data destination via an output stream
object
Recall hatin the last section in Unit 2, abstract classes are superclasses which contain
abstract methods and are defined such that concrete subclasses are to extend them
by implementing the methods. The behaviours defined by such a class are “generic”
and much of che class will be undefined and unimplemented. In Figure 3.5, the
class name outputstream is set in italics to highlight that there are no objects of
the class OutputStream because it isan abstract class. Instead, it isan object ofa
concrete subclass, outputsezeam that associates with a particular data destination
at runtime, Calling the weite () method of the object with supplementary data
in units of type byte will ousput the data to its associated destination (such as the
data are written to a file for a FiLeoutputstream object)
You should notice that the parameter type of the write () method is tot but
the data co be written to the data destination are byte. Therefore, only the least
significant eight bits of the supplied data of type int are written to the associated
data destination