DSC 6 12

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 13

I/0 in Java

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

Java programs perform I/O through streams.

A stream is an abstraction that either produces or consumes information, A stream


is linked to a physical device by the Java I/O system.

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 and Character Streams

Java defines two types of streams: byte and character.

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.

They use Unicode and, therefore, can be internationalized.

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.

Both methods are declared as abstract inside InputStream and OutputStream.


They are overridden by derived stream classes.
Character Stream Class

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.

These methods are overridden by derived stream classes.


Predefined Streams

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.in refers to standard input, which is the keyboard by default.

System.err refers to the standard error stream, which also is the console by default.

However, these streams may be redirected to any compatible I/O device.

System.in is an object of type InputStream; System.out and System.err are objects


of type PrintStream.

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.

However, Java allows us to wrap a byte-oriented file stream within a character-


based object.

Two of the most often-used stream classes are FileInputStream and


FileOutputStream, which create byte streams linked to files.

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.

FileInputStream(String fileName) throws FileNotFoundException


FileOutputStream(String fileName) throws FileNotFoundException
Reading and Writing Files

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.

For output streams, if the file cannot be created, 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( ).

It is defined by both FileInputStream and FileOutputStream, as:


void close( ) throws IOException
Reading and Writing Files

To read from a file, we can use a version of read( ) that is defined within
FileInputStream.

The one that we will use is:


int read( ) throws IOException

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.

It can throw an IOException.


Reading and Writing Files
import java.io.*;
class ShowFile {
public static void main(String args[])
throws IOException
{
int i;
FileInputStream fin;
try {
fin = new FileInputStream(args[0]);
} catch(FileNotFoundException e) {
System.out.println("File Not Found");
return;
} catch(ArrayIndexOutOfBoundsException e) {
System.out.println("Usage: ShowFile File");
return;
}
do { i = fin.read();
if(i != -1) System.out.print((char) i);
} while(i != -1);
fin.close();
}
}

You might also like