INTRODUCTION :
Stream is an abstract demonstration of input or output device. By using stream, we can write or read data. To bring in information, a program is open a stream on an information source (a file, memory, a socket) and read information sequentially. In this unit, we will learn the concept of stream, I/O package.
STREAM:
The Java Input/Output (I/O) is a part of java.io package. The java.io package contains a relatively large number of classes package are primarily abstract classes and stream-oriented that define methods and subclasses which allow bytes to be read from and written to files or other input and output sources.
For reading the stream :
There are two types of stream as follows :
It supports 8-bit input and output operations. There are two classes of byte stream
The InputStream class is used for reading the data such as a byte and array of bytes from an input source. An input source can be a file, a string, or memory that may contain the data. It is an abstract class that defines the programming interface for all input streams that are inherited from it. An input stream is automatically opened when you create it. You can explicitly close a stream with the close( ) method, or let it be closed implicitly when the object is found as a garbage.
The subclasses inherited from the InputStream class can be seen in a hierarchy manner shown below:
Input Stream :
The OutputStream class is a sibling to InputStream that is used for writing byte and array of bytes to an output source. Similar to input sources, an output source can be anything such as a file, a string, or memory containing the data. Like an input stream, an output stream is automatically opened when you create it. You can explicitly close an output stream with the close( ) method, or let it be closed implicitly when the object is garbage collected.
The classes inherited from the OutputStream class can be seen in a hierarchy structure shown below:
Output Stream
OutputStream is also inherited from the Object class. Each class of the OutputStream provided by the java.io package is intended for a different purpose.
It supports 16-bit Unicode character input and output. There are two classes of character stream as follows:
These classes allow internationalization of Java I/O and also allow text to be stored using international character encoding.
Reader :
Writer :
Java uses streams to handle I/O operations through which the data is flowed from one location to another. For example, an InputStream can flow the data from a disk file to the internal memory and an OutputStream can flow the data from the internal memory to a disk file. The disk-file may be a text file or a binary file. When we work with a text file, we use a character stream where one character is treated as per byte on disk. When we work with a binary file, we use a binary stream.
The working process of the I/O streams can be shown in the given diagram.
The following lists of classes are provided by the java.io package shown in the table:
Class | Description |
---|---|
BufferedInputStream | It used for creating an internal buffer array. It supports the mark and reset methods. |
Buffered Output Stream | This class used for writes byte to output stream. It implements a bufferedoutput stream. |
Buffered Reader | This class provides read text from character input stream and buffering characters. It also reads characters, arrays and lines. |
Buffered Writer | This class provides write text from character output stream and buffering characters. It also writes characters, arrays and lines. |
ByteArrayInput Stream | It contains the internal buffer and read data from the stream. |
ByteArrayOutput Stream | This class used for data is written into byte array. This is implemented in output stream class. |
CharArrayReader | It used for char input stream and implements a character buffer. |
CharArrayWriter | This class also implements a character buffer and it uses an writer. |
DataInput Stream | This class reads the primitive data types from the input stream in a machine format. |
DataOutputStream | This class writes the primitive data types from the output stream in machine format. |
File | This class shows a file and directory pathnames. |
File Descriptor | This class uses for create a FileInputStream and FileOutputStream. |
FileInputStream | It contains the input byte from a file and implements an input stream. |
FileOutputStream | It uses for writing data to a file and also implements an output stream. |
FilePermission | It provides the permission to access a file or directory. |
FileReader | This class used for reading characters file. |
FileWriter | This class used for writing characters files. |
InputStream | This class represents an input stream of bytes. |
InputStreamReader | It reads bytes and decodes them into characters. |
LineNumberReader | This class has a line numbers |
ObjectInputStream | This class used for recover the object to serialize previously. |
ObjectInputStream.GetField | This class access to president fields read from input stream. |
ObjectOutputStream | This class used for writing the primitive data types and also to write the object to read by the ObjectInputStream. |
ObjectStreamClass | Serialization's descriptor for classes. |
ObjectStreamField | This class describes the serializable field. |
OutputStream | This class represents an output stream of bytes. |
OutputStreamWriter | It writes bytes and decodes them into characters. |
StringReader | This is a character string class. It has character read source. |
StringWriter | This is also a character string class. It uses to shows the output in the buffer. |
Writer | It uses for writing to character stream. |
The following summary of the exception classes provided by the java.io package shown in the table :
Exceptions | Description |
---|---|
Char Conversion Exception | It provides detail message in the catch block to associated with the CharConversionException |
EOF Exception | This exception indicates the end of file. When the file input stream is to be end then the EOFException is to be occured. |
FileNotFound Exception | When the opened file's pathname does not find then this exception occurs. |
InterruptedIO Exception | When the I/O operations are interrupted from any causes then it occurs. |
InvalidClassException | Any problems to be created with class, when the Serializing runtime to be detected. |
InvalidObject Exception | When the de-serialized objects fails then it occurs. |
IOException | When the I/O operations fail then it occurs. |
NotActive Exception | The Serialization or deserialization operations are not active then it occurs. |
NotSerializable Exception | This exception occurs when the instance is required to be a Serializable interface. |
ObjectStream Exception | This is a supper class of all exception class. It is used for specific Object Stream Classes. |
WriteAborted Exception | In this exception to be thrown by the ObjectStreamException during a write operating. |
Standard Streams are a feature provided by many operating systems. By default, they read input from the keyboard and write output to the display. They also support I/O operations on files.
Java also supports three Standard Streams :
These objects are defined automatically and do not need to be opened explicitly.
Standard Output and Standard Error, both are to write output; having error output separately so that the user may read error messages efficiently.
System.in is a byte stream that has no character stream features. To use Standard Input as a character stream, wrap System.in within the InputStreamReader as an argument.
InputStreamReader inp= new InputStreamReader (System.in);
We are going to see READER CLASSES in next topic..
All Rights Reserved. © 2024 BookOfNetwork