A SEMINAR REPORT ON I/O STREAM CLASSES
Submitted by: Shelly Maheshwari
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.
output from a program may go to the screen. SocketInputStream. Your program can produce output by writing a sequence of characters to an output stream attached to a destination. 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.
A stream is data that you access in sequence. treating data as a sequence of bytes or characters and support to the sequential access to the data. the printer. All the other streams just add capabilities to the basics. or another program. the disk.CONCEPT OF STREAM
Definition: Stream is flowing sequence of characters. Or sticks floating down a river while you watching from a bridge. 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. the disk. they share
common characteristics such as unidirectional movement of data. ServletInputStream. 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 java’s native Unicode(Reader).
. the memory. Similarly. the memory. the mouse. and so on. or say where the data is coming from (FileInputStream. Although these devices look different the hardware level.Input to a program may come from the keyboard. the other letting us put data out to somewhere. Your program can get input from a data source by reading a sequence characters from a stream attached to the source. InputStream and OutputStream are the basic stream classes in java. a network.
ByteInputStream. etc). another program. a network.
A stream in java is a path along which data flows (like a river or a pipe along which water flows).INPUT
RELATIONSHIP OF JAVA PROGRAM WITH I/O DEVICES. 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.
reads) data from the source (file) and sends it to the program.
READING DATA INTO A PROGRAM.e. Similarly.
. input stream and output stream.
WRITES OUTPUT STREAM
WRITING DATA TO A DESTINATION. an output streams takes data from the program and sends (i.e. namely. writes) it to the destination (file). An input stream extract (i. 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.io package contains a large number of stream classes that provide capabilities for processing all types of data.
The java.Java streams are classified into two types.
We can also cross-group the stream based on type of source or destination they read from or write to. Byte stream and character stream classes contain specialized classes to deal with input and output operations independently on various types of devices.
. a file or a pipe.2) Character stream classes that provide support for managing I/O operation on characters. Figure shows how stream classes are grouped based on their functions. These two groups may further be classified based on their purpose. The source (or destination) may be memory.
. CLASSIFICATION OF JAVA STREAM CLASSES.
BYTE STREAM CLASSES
The byte stream classes provide a rich environment for handling byte-oriented I/O. Since the streams are unidirectional.
. Java provides two kinds of byte stream classes: input stream classes and output stream classes. therefore. including binary data. A byte can be used with any type of object. they can transmit bytes in only one directional and.Byte stream classes have been designed to provide functional features for creating and manipulating streams and files for reading and writing bytes.
.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 java’s model of streaming byte input.
and therefore. we cannot create instances of this class. Reads m bytes into b starting from nth byte. Rather. 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
Read ( ) Read (byte b[ ]) Read (byte b[ ]. we must use the subclasses that inherit from this class. int m) Available( ) Skip(n) Reset( ) Close( )
Reads the byte from the input stream. Skip over n bytes from the input stream.
HIERARCHY OF INPStreUT STREAM CLASSES. int n.
The super class Inputstream is an abstract class. Closes the input stream. Reads an array of bytes into b. Goes back to the beginning of the stream. Gives number of bytes available in the input.
. Writes all bytes in the array b to the output stream. 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. the Outputstream is an abstract class and therefore we cannot instantiate it. int n. int m) close( )
Writes m bytes from array b starting from nth byte.
write (byte b[ ].OUTPUT STREAM CLASSES
Output stream classes are derived from the base classes OutputStream as shown in below figure. Like InputStream. Closes the output stream. The several subclasses of the OutputStream can used for performing the output operations.
writeShort( ) writeInt( ) writeLong( ) writeFloat( ) writeDouble( ) writeBytes( ) writeChar( ) writeBoolean()
HIERARCHY OF OUTPUT STREAM CLASSES
The DataOutputStream. therefore. implements the interface DataOutput and. implements the following methods contained in DataOutput interface. a counter part of DataInputStream.
Character stream can be used to read and write 16-bit Unicode characters.CHARACTER STREAM CLASSES
Character stream classes were not part of the language when it is released in 1995. Since one of main purpose of java is to support the “write once.1 was announced. There are two kinds of character stream classes namely reader stream classes and writer stream classes.
. it was necessary to include direct I/O support for characters. run anywhere” philosophy. They were added later when the version 1.
except characters. reader classes can perform all the functions implemented by the input stream classes. except input steams uses bytes as their fundamental unit of information. while reader streams use characters. These classes are functionally very similar to the input stream classes. The Reader class contains methods that are identical to those available in the InputStream class. Reader is designed to handle
. Reader class is the base class for all other classes in this group. Therefore.READER STREAM CLASSES
Reader stream classes are designed to create to read character from the files.
the writer stream classes are designed to perform all output operations on files.WRITER STREAM CLASSES
Like output stream classses.
. Only difference is that while output stream classes designed to write bytes. 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. the writer stream classes are designed to write characters.
data” into a file called “output.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).dat”. PROGRAM: COPYING CHARACTERS
. Uses theses two file stream classes to copy the contents of a file named “input.
This program is very simple.dat” and “output. It creates two files objects inFile and outfile and initilizes them with “input.dat” respectively using the following code”
READING FROM AND WRITING TO FILES
As pointed out earlier. However. java i/o system provides a number of classes that handle 8-bit bytes. We can use them in place of FileReader and FileWriter. PROGRAM: COPYING BYTES
We have used fileReader and fileWriter classes to read and write 16-bit characters. Two commonly used classes for handling bytes are FileInputSteam and FileOutputStream classes. most file systems use only 8-bit bytes.