Core JAVAWrite Once, Run Anywhere |
The Java I/O package contains nearly every class you might ever need to perform input and output (I/O) in Java. All these streams represent an input source and an output destination. The stream in the java.io package supports many data such as primitives, Object, localized characters etc.
A stream can be defined as a sequence of data. The InputStream is used to read data from a source and the OutputStream is used for writing data to a destination.
Java does provide strong, flexible support for I/O as it relates to files and networks but this tutorial covers very basic functionlity related to streams and I/O. We would see most commonly used example one by one:
Reading Console Input:
Java input console is accomplished by reading from System.in. To obtain a character-based stream that is attached to the console, you wrap System.in in a BufferedReader object, to create a character stream. Here is most common syntax to obtain BufferedReader:
Once BufferedReader is obtained, we can use read( ) method to reach a character or readLine( ) method to read a string from the console.
Reading Characters from Console:
To read a character from a BufferedReader, we would read( ) method whose sytax is as follows:
Each time that read( ) is called, it reads a character from the input stream and returns it as an integer value. It returns .1 when the end of the stream is encountered. As you can see, it can throw an IOException.
The following program demonstrates read( ) by reading characters from the console until the user types a "q":
Output:
Reading Strings from Console:
To read a string from the keyboard, use the version of readLine( ) that is a member of the BufferedReader class. Its general form is shown here:
The following program demonstrates BufferedReader and the readLine( ) method. The program reads and displays lines of text until you enter the word "end":
Example:
Output:
Writing Console Output:
Console output is most easily accomplished with print( ) and println( ), described earlier. These methods are defined by the class PrintStream which is the type of the object referenced bySystem.out. Even though System.out is a byte stream, using it for simple program output is still acceptable.
Because PrintStream is an output stream derived from OutputStream, it also implements the low-level method write( ). Thus, write( ) can be used to write to the console. The simplest form of write( ) defined by PrintStream is shown here:
This method writes to the stream the byte specified by byteval. Although byteval is declared as an integer, only the low-order eight bits are written.
Example:
Here is a short example that uses write( ) to output the character "A" followed by a newline to the screen:
Output:
Note: You will not often use write( ) to perform console output because print( ) and println( ) are substantially easier to use.
Reading and Writing Files:
As described earlier, A stream can be defined as a sequence of data. The InputStream is used to read data from a source and the OutputStream is used for writing data to a destination. Here is a hierarchy of classes to deal with Input and Output streams.
FileInputStream:
This stream is used for reading data from the files. Objects can be created using the keyword new and there are several types of constructors available.
Following constructor takes a file name as a string to create an input stream object to read the file:
Following constructor takes a file object to create an input stream object to read the file. First we create a file object using File() method as follows:
Once you have InputStream object in hand then there is a list of helper methods which can be used to read to stream or to do other operations on the stream.
There are other important input streams available, for more detail you can refer to the following links:
ByteArrayInputStream:
The ByteArrayInputStream class allows a buffer in the memory to be used as an InputStream. The input source is a byte array. There are following forms of constructors to create ByteArrayInputStream objects.
Takes a byte array as the parameter:
Another form takes an array of bytes, and two ints, where off is the first byte to be read and len is the number of bytes to be read.
Once you have ByteArrayInputStream object in hand then there is a list of helper methods which can be used to read the stream or to do other operations on the stream.
DataInputStream:
The DataInputStream is used in the context of DataOutputStream and can be used to read primitives. Following is the constructor to create an InputStream:
Once you have DataInputStream object in hand then there is a list of helper methods which can be used to read the stream or to do other operations on the stream.
|
FileOutputStream:
FileOutputStream is used to create a file and write data into it.The stream would create a file, if it doesn't already exist, before opening it for output. Here are two constructors which can be used to create a FileOutputStream object.
Following constructor takes a file name as a string to create an input stream object to write the file:
Once you have OutputStream object in hand then there is a list of helper methods which can be used to write to stream or to do other operations on the stream.
|
ByteArrayOutputStream:
The ByteArrayOutputStream class stream creates a buffer in memory and all the data sent to the stream is stored in the buffer. There are following forms of constructors to create ByteArrayOutputStream objects.
Following constructor creates a buffer of 32 byte:
Following constructor creates a buffer of size int a:
File Navigation and I/O:
There are several other classes that we would be going through to get to know the basics of File Navigation and I/O.
File Class :
Java File class represents the files and directory pathnames in an abstract manner. This class is used for creation of files and directories, file searching, file deletion etc. The File object represents the actual file/directory on the disk. There are following constructors to create a File object:
Following syntax creates a new File instance from a parent abstract pathname and a child pathname string.
Following syntax creates a new File instance by converting the given pathname string into an abstract pathname.
Following syntax creates a new File instance from a parent pathname string and a child pathname string.
Following syntax creates a new File instance by converting the given file: URI into an abstract pathname.