DSC 6 12
DSC 6 12
DSC 6 12
DSC – 6 – 12
I/0 in Java
The I/O package supports Java’s basic I/O (input/output) system, including file I/O.
The Applet package supports applets, Support for both I/O and applets comes from
Java’s core API libraries, not from language keywords.
Streams
All streams behave in the same manner, even if the actual physical devices to
which they are linked differ.
I/0 in Java
Thus, the same I/O classes and methods can be applied to any type of device.
This means that an input stream can abstract many different kinds of input: from a
disk file, a keyboard, or a network socket.
Likewise, an output stream may refer to the console, a disk file, or a network
connection.
Streams are a clean way to deal with input/output without having every part of our
code understand the difference between a keyboard and a network.
for example. Java implements streams within class hierarchies defined in the
java.io package
I/0 in Java
Byte streams provide a convenient means for handling input and output of bytes.
Byte streams are used, for example, when reading or writing binary data.
Character streams provide a convenient means for handling input and output of
characters.
Also, in some cases, character streams are more efficient than byte streams.
I/0 in Java
Byte Stream Class
Byte streams are defined by using two class hierarchies, at the top are two abstract
classes: InputStream and OutputStream.
Each of these abstract classes has several concrete subclasses that handle the
differences between various devices, such as disk files, network connections, and
even memory buffers.
The abstract classes InputStream and OutputStream define several key methods
that the other stream classes implement, Two of the most important are read( )
and write( ), which, respectively, read and write bytes of data.
Character streams are defined by using two class hierarchies, at the top are two
abstract classes, Reader and Writer.
These abstract classes handle Unicode character streams, Java has several concrete
subclasses of each of these.
The abstract classes Reader and Writer define several key methods that the other
stream classes implement.
Two of the most important methods are read( ) and write( ), which read and write
characters of data, respectively.
Java programs automatically import the java.lang package, this package defines a
class called System, which encapsulates several aspects of the run-time
environment.
For example, using some of its methods, we can obtain the current time and the
settings of various properties associated with the system.
System also contains three predefined stream variables: in, out, and err.
These fields are declared as public, static, and final within System, means that they
can be used by any other part of your program and without reference to a specific
System object.
Predefined Streams
System.out refers to the standard output stream, by default, this is the console.
System.err refers to the standard error stream, which also is the console by default.
These are byte streams, even though they typically are used to read and write
characters from and to the console.
Reading and Writing Files
In Java, all files are byte-oriented, and Java provides methods to read and write
bytes from and to a file.
To open a file, you simply create an object of one of these classes, specifying the
name of the file as an argument to the constructor.
Here, fileName specifies the name of the file that we want to open.
When we create an input stream, if the file does not exist, then
FileNotFoundException is thrown.
When an output file is opened, any preexisting file by the same name is destroyed.
When you are done with a file, you should close it by calling close( ).
To read from a file, we can use a version of read( ) that is defined within
FileInputStream.
Each time that it is called, it reads a single byte from the file and returns the byte as
an integer value. read( ) returns – 1 when the end of the file is encountered.