You are on page 1of 27

A SEMINAR REPORT ON I/O STREAM CLASSES

Submitted by: Shelly Maheshwari

INTRODUCTION
We have used variables and arrays for storing data inside the program. This approach poses the following problem: 1) The data is lost either when a variable goes out of scope or when the program is terminated. That is, the storage is temporary. 2) It is difficult to handle large volumes of data using variables and arrays. We overcome these problems by storing data on secondary storage devices such as floppy disks or hard disks. The data is stored in these devices using the concept of files. Data stored in files is often called persistent data. A file is a collection of related records placed in a particular area on the disk. A record is composed of several fields and a field is a group of characters. Characters in java are Unicode characters composed of two bytes, each byte containing eight binary digits, 1 or 0. Storing and managing data using data files is known as file processing which include tasks such as creating files, updating files and manipulation of data. Java supports many powerful features for managing input and output of data using files. Reading and writing of data in a file can be done at the level of bytes or characters or fields depending on the requirements of a particular application. Java Also provides capabilities to read and write class objects directly. Note that a record may be represented as a class object in java. The process of reading and writing objects is called object serialization.

CONCEPT OF STREAM
Definition: Stream is flowing sequence of characters.
A stream is data that you access in sequence. You could think of it like a train that you watch from a tunnel entrance so that you can just see one car at a time. Or a stream of widget coming across a conveyor belt requiring you to tighten a screw on each one before it passes by to the next person down the assembly line who has to pound it with a hammer, and so on. Or sticks floating down a river while you watching from a bridge. InputStream and OutputStream are the basic stream classes in java, the other letting us put data out to somewhere. All the other streams just add capabilities to the basics, like the ability to read a whole chunk of data at once for performance reasons(BufferedInputStream) or convert from one kind of character set to javas native Unicode(Reader), or say where the data is coming from (FileInputStream, ServletInputStream, SocketInputStream,

ByteInputStream, etc). Your program can get input from a data source by reading a sequence characters from a stream attached to the source. Your program can produce output by writing a sequence of characters to an output stream attached to a destination.Input to a program may come from the keyboard, the mouse, the memory, the disk, a network, or another program. Similarly, output from a program may go to the screen, the printer, the memory, the disk, a network, another program. Although these devices look different the hardware level, they share

common characteristics such as unidirectional movement of data, treating data as a sequence of bytes or characters and support to the sequential access to the data.

SOURCES

DESTINATIONS

KEYBOARD

SCREEN PRINTER

MOUSE
JAVA

PRINTER

MEMORY

PROGRAM

MEMORY

INPUT

OUTPUT

RELATIONSHIP OF JAVA PROGRAM WITH I/O DEVICES.

A stream in java is a path along which data flows (like a river or a pipe along which water flows). It has a source (of data) and a destination (for that data) as shown in given figure. Both the source and destination may be physical devices or program or other stream in the same program

CONCEPTUAL VIEW OF A STREAM.

Java streams are classified into two types, namely, input stream and output stream. An input stream extract (i.e. reads) data from the source (file) and sends it to the program. Similarly, an output streams takes data from the program and sends (i.e. writes) it to the destination (file).

INPUT STREAM

SOURCE

READS

PROGRAM

READING DATA INTO A PROGRAM.


WRITES OUTPUT STREAM

PROGRAM

DESTINATION

WRITING DATA TO 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 types on which they operate. 1) Bytes stream classes that support for handling I/O operation on bytes.

2) Character stream classes that provide support for managing I/O operation on characters. These two groups may further be classified based on their purpose. Figure shows how stream classes are grouped based on their functions. Byte stream and character stream classes contain specialized classes to deal with input and output operations independently on various types of devices. We can also cross-group the stream based on type of source or destination they read from or write to. The source (or destination) may be memory, a file or a pipe.

CLASSIFICATION OF JAVA STREAM CLASSES.

BYTE STREAM CLASSES

The byte stream classes provide a rich environment for handling byte-oriented I/O. A byte can be used with any type of object, including binary data.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 directional and, therefore, Java provides two kinds of byte stream classes: input stream classes and output stream classes.

INPUT STREAM CLASSES


Input stream classes that are used to read 8-bit bytes include a super class known as InputStream and a number of subclasses for supporting various input-related functions. InputStream is an abstract class that defines javas model of streaming byte input.

Object

InputStream

FileInputStream

SequenceInputStream

ByteArrayInputStrea m

ObjectInputStream

FilterInputStream

BufferedInputam

PushbackInputStream

HIERARCHY OF INPStreUT STREAM CLASSES.


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 function such as Reading bytes Closing streams Marking position in streams Skipping ahead in a stream Finding the number of bytes in a stream

METHOD
Read ( ) Read (byte b[ ]) Read (byte b[ ], int n, int m) Available( ) Skip(n) Reset( ) Close( )

DESCRIPTION
Reads the byte from the input stream. Reads an array of bytes into b. Reads m bytes into b starting from nth byte. Gives number of bytes available in the input. Skip over n bytes from the input stream. Goes back to the beginning of the stream. Closes the input stream.

OUTPUT STREAM CLASSES


Output stream classes are derived from the base classes OutputStream as shown in below figure. Like InputStream, the Outputstream is an abstract class and therefore we cannot instantiate it. The several subclasses of the OutputStream can used for performing the output operations. The OutputStream includes methods that are designed to perform the following task: Writing bytes Closing streams Table gives a brief description of all methods defined by the OutputStream class. METHOD DESCRIPTION

write( ) write (byte b[ ])

Writes a byte to the output stream. Writes all bytes in the array b to the output stream.

write (byte b[ ], int n, int m) close( )

Writes m bytes from array b starting from nth byte. Closes the output stream.

Object

OutputStream

FileOutputStream

ByteArrayOutputStream

FilterOutputStream

BufferedOutputStream

PushbackOutputStream

HIERARCHY OF OUTPUT STREAM CLASSES


The DataOutputStream, a counter part of DataInputStream, implements the interface DataOutput and, therefore, implements the following methods contained in DataOutput interface. writeShort( ) writeInt( ) writeLong( ) writeFloat( ) writeDouble( ) writeBytes( ) writeChar( ) writeBoolean()

CHARACTER STREAM CLASSES


Character stream classes were not part of the language when it is released in 1995. They were added later when the version 1.1 was announced. Character stream can be used to read and write 16-bit Unicode characters. Since one of main purpose of java is to support the write once, run anywhere philosophy, it was necessary to include direct I/O support for characters. There are two kinds of character stream classes namely reader stream classes and writer stream classes.

READER STREAM CLASSES


Reader stream classes are designed to create to read character from the files. Reader class is the base class for all other classes in this group. These classes are functionally very similar to the input stream classes, except input steams uses bytes as their fundamental unit of information, while reader streams use characters. The Reader class contains methods that are identical to those available in the InputStream class, except characters. Therefore, reader classes can perform all the functions implemented by the input stream classes. Reader is designed to handle

WRITER STREAM CLASSES


Like output stream classses, the writer stream classes are designed to perform all output operations on files. Only difference is that while output stream classes designed to write bytes, the writer stream classes are designed to write characters. The Writer class is an abstract class which acts as a base class for all the other writer stream classes. This base class provides support for aall output operations by defining methods that area identical to those in OutputStream class.

READING/WRITING CHARACTERS
Subclasses of Reader and Writer implements streams that can handle characters. The two subclasses used for handling characters in files are FileReader (for reading characters) and Filewriter (for writing characters). Uses theses two file stream classes to copy the contents of a file named input.data into a file called output.dat. PROGRAM: COPYING CHARACTERS

This program is very simple. It creates two files objects inFile and outfile and initilizes them with input.dat and output.dat respectively using the following code

READING FROM AND WRITING TO FILES

READING/WRITING BYTES
We have used fileReader and fileWriter classes to read and write 16-bit characters. However, most file systems use only 8-bit bytes. As pointed out earlier, java i/o system provides a number of classes that handle 8-bit bytes. Two commonly used classes for handling bytes are FileInputSteam and FileOutputStream classes. We can use them in place of FileReader and FileWriter. PROGRAM: COPYING BYTES

You might also like