HANDLING I/O IN JAVA!

1. Unidirectional movement of data
2. Treating data as a sequence of bytes or characters and
3. Support of sequential access to the data
Hence, Java uses the concept of streams to represent the ordered sequence of data, a common characteristic shared by all I/O devices. A stream presents a uniform, easy-to-use, object-oriented interface between the program and I/O devices.
A stream in Java is a path along which data flows. It has a source and a destination. The source and the destination may refer to physical devices or programs or other streams in the same program. Java streams are classified into two basic types namely Input stream and Output stream.
An input stream extracts (i.e. reads) data from the source and send them to the program. Similarly, an output stream takes data from the program and sends (i.e. writes) them to the destination. In both cases, the program doesn’t know the details of end points (i.e., source and destination).
Stream Classes:
To support stream oriented I/O, Java provides java.io package, which contains a large number of stream classes that provide capabilities for all types of I/O. These classes may be grouped into two groups based on the data type on which they operate:
1. Byte stream classes that provide support for handling I/O on bytes and
2. Character stream classes that provide support for managing I/O on characters
These two groups may be further classified based on their purpose. Byte stream classes have been designed to provide functional features for reading and writing bytes of data from streams and files; whereas character stream classes are designed to read and write 16-bit Unicode characters.
Byte Streams:
The package java.io provides two set of class hierarchies - one for handling reading and writing of bytes, and another for handling reading and writing of characters. The abstract classes InputStream and OutputStream are the root of inheritance hierarchies handling reading and writing of bytes respectively.
The java.io package contains several subclasses of InputStream and OutputStream that implement specific input/output functions. For example, FileInputStream and FileOutputStream are input and output stream classes that operate on files on the native file system. Similarly Reader and Writer classes are streams that operate on character input and output respectively.
Three Objects for I/O:
The java.io package contains several subclasses of InputStream and OutputStream that implement specific input/output functions. For example, FileInputStream and FileOutputStream are input and output stream classes that operate on files on the native file system. Similarly Reader and Writer classes are streams that operate on character input and output respectively.
Three Objects for I/O:
Three objects are provided in the System object for handling the input, output and error in Java. They are named as in, out and err and are of type static. The object ‘in’ is of InputStream type and the other two are of PrintStream type. With the help of these objects, we can get the input from the user through keyboard, send the output to the screen, and display error messages if any error occurs. The root object – System is available in the package java.lang, and is accessible by default from every Java program.
Data to be stored in a variable can be obtained through the keyboard using the readLine() method of the DataInputStream object. The readLine() method reads the input from the keyboard as a string that can be converted to another data type using the wrapper class provided for the corresponding data type.
Java provides five primitive data types for data storage, namely int, float, long, char and double. It also provides wrapper classes in its java.lang package for converting primitive data types into object data type. The wrapper classes help the user store their data in objects. Such classes are listed below:
Primitive Datatype | Wrapper Classes |
Boolean | Boolean |
Char | Character |
Double | Double |
Float | Float |
Int | Integer |
Long | Long |
Table : Wrapper Classes for Primitive Data types in Java
The following Java program illustrates the use of Wrapper Classes for converting scalar data into an object:
class Reading {
public static void main(String args[]){
DataInputStream in = new DataInputStream(System.in);
int intNo = 0;
float floatNo = 0.0f;
try
{
System.out.println(“Enter an Integer:”);
intNo = Integer.parseInt(in.readLine());
System.out.println(“Enter a float No.”);
floatNo = Float.valueOf(in.readLine()).floatValue();
}
catch(Exception e)
{}
System.out.println(“intNumber = “ + intNo);
System.out.println(“floatNumber = “ + floatNo);
}
}
In the above java program, user input is obtained twice with the help of the readLine() method of the input (in) object. The given input is in the form of string of characters, which is then converted into integer and float using the wrapper classes Integer and Float respectively. The method parseInt() of Integer class converts the string data into an integer value. Similarly, the valueOf() method of the Float class converts the input data into a floating value. The method floatValue() of the Float class retrieves the floating point value in a floating-point notation.