MANAGING INPUT/OUTPUT
FILES IN JAVA
FILE HANDLING
CONCEPT OF STREAMS:
In file processing , input refers to flow of data into a program and
output means the flow of data out of a program
input to a program may come from the keyboard ,the mouse, the memory, the
disk, a network, another program.
similarly, output from a program may go to the screen, the printer, the
memory, the disk, a network, or another
program.
STREAM CLASS:
A stream in java in path along which data flows(LIKE A RIVER OR A PIPE
ALONG WHICH WATER FLOWS). It has a
source (OF DATA) and a destination.
The concept of sending data from one stream to another(LIKE ONE PIPE
FEEDING INTO ANOTHER PIPE) has made
streams in java a powerful tool for file processing.
Input
SOURCE
PROGRAM
(a) Reading data into a program
PROGRAM
DESTINATION
(b) Writing data into a
destination
STREAM CLASSES:
The java.io package contains a large number of stream classes that
provide capabilities for
processing all types of data. These classes may be categorized into two
groups based on the
data type on which they operate:
1. Byte stream classes that provide support for handling I/O operations
on bytes.
2. Character stream classes that provide support for managing I/O
operations on characters.
Java Stream Classes
Character
Byte Stream
Stream
Classes
Classes
Input Stream Output Stream
Reader C lasses Writer Classes
C lasses Classes
Memory
Memory File Pipe
File Pipe
Classific ation of Java Stream
Classes
Byte Stream Classes
• Byte Stream Classes have been designed to provide functional features
for creating and
manipulating streams and files for reading and writing bytes.
• Since the streams are unidirectional, they can transmit bytes in only
one direction and
therefore, Java provides two kinds of byte stream classes:
Input Stream Classes
• Input stream classes that are used to read 8-bit bytes include a super
class known as
Input Stream and a number of subclasses for supporting various input
related functions.
• The super class InputStream is an abstract class and therefore we
cannot create
instances of this class. Rather, we must use the subclasses that
inherit from this class
The InputStream class defines methods for performing input functions such
as
• Reading bytes
• Closing streams
• Marking positions in streams
• Skipping ahead in a stream
• Finding the number of bytes in stream
Summary of InputStream methods:
Method Description
1. read() Reads a byte from the input stream.
2. available() Gives number of bytes available in
the input.
3. skip(n) Skips over n bytes from the input stream.
4. reset() Goes back to the beginning of the stream.
5. close() Closes the input stream.
Output Stream Classes
ὸ Output stream classes are derived from the base class OutputStream.
ὸ Like InputStream the OutputStream is an abstract class and therefore
we cannot instantiate
it.
ὸ The several subclasses of the OutputStream can be used for performing
the output
operations.
ὸ The OutputStream includes methods that are designed to perform the
following tasks
Summary of OutputStream methods:
Method Description
1. write() Writes a byte to the output stream.
2. write(byte b[]) Writes all bytes in the array b to the
output stream
3. write(byte b[], int n, int m) Writes m bytes from array b starting
from the nth byte
4. close() Closes the output stream.
5. flush() Flushes the output stream.
CHARACTER STREAM CLASSES
ὸ Character streams can be used to read and write 16-bit Unicode
character. Like byte streams, there
are two kinds of character stream classes, namely reader stream
classes and writer stream classes.
Reader class is the base class
for all other classes in this group.
classes except input streams use bytes
as their fundamental unit of information, while reader streams use
characters.
s contains methods that are identical to those
available in the InputStream class,
except Reader is designed to handle characters.
operations o files
Writer stream classes are designed tot write characters.
Hierarchy of reader stream classes:
Object
Reader
BufferedReader
StringReader
CharArrayReader PipeReader
InputStreamReader FilterReader
FileReader PushbackReader