Introduction to Java Stream OR java.io package
- Working process of the I/O streams
- InputStream Class Hierarchy
- OutputStream Class Hierarchy
- Readers and Writers
- The File Class
- File Input and Output
- Buffered I/O classes
- Character Streams
- The RandomAccessFile Class
- A channel for sending information in first-in, first-out manner.
- It is a sequence of bytes traveling from a source (if program writes ) to a destination ( if program reads ) over a communication path.
Can be classified as:
- input streams – used for reading
- output streams – used for writing
- filter streams – used to read or write data from one stream to another stream
- node streams – used to read or write to a specific place like a disk, a file or the memory
Stream of data flowing to a destination from source.
|Working process of the I/O streams|
The working process of the I/O streams can be shown in the given diagram.
In java.io package there are two abstract class InputStream and OutputStream for Stream Classes
Note :- java.io package Supports system input and output, object serialization, and basic access to the file system.
|InputStream Class Hierarchy|
Has the InputStream class at the top of the hierarchy.
InputStream Class Methods
- int read() – reads a byte of data from the input stream
- int read(byte b) – reads bytes of data from the input stream and stores them in the array
- int read(byte b, int off, int len) – reads the number of bytes specified by the third argument from the input stream and stores it in the array. The second argument specifies the position from where the bytes have to be read
- int available() – returns the number of bytes available for reading from the input stream
- void close() – closes an input stream and releases all the resources associated with it
Is shown in the figure given below:
|OutputStream Class Hierarchy|
Has the OutputStream class at the top of the hierarchy
OutputStream Class Methods
- void write(int n) – writes the specified byte of data to the output stream
- void write(byte b) – writes an array of bytes to the output stream
- void write(byte b, int off, int len) – Writes a segment of an array to the output stream
- void flush() – force writes whenever the data accumulates in the output stream
- void close() – causes the output stream to close. Any data present on it is stored before the stream is deallocated from memory
It is shown in the figure as given below:
|Readers and Writers|
- The Reader and the Writer classes are abstract classes
- They support the reading and writing of Unicode character streams in which each character is represented by 16 bits
- The InputStreamReader class converts an InputStream subclass object into a Unicode character stream
- The OutputStreamWriter class converts a Unicode character output stream into a byte output stream
- Buffered character input/output is supported by the BufferedReader and BufferedWriter classes
- The readLine() method of the BufferedReader class is used for reading lines of text from the console, the file, or other input streams
Reader Class Hierarchy :
Writer Class Hierarchy :
|The File Class|
The File class provides access to file and directory objects has methods to manipulate them.
public class File extends Object implements Serializable, Comparable
It has the following constructors:
- File(String pathname) – creates a new instance of the File class by converting the given pathname string into an abstract pathname
- File(String parent, String child) – creates a new instance of the File class from a parent pathname and a child pathname strings
- File(File parent, String child) – creates a new File instance from a parent File object and a child pathname
|File Input and Output|
- The FileInputStream and FileOutputStream classes provide the capability to read and write file streams.
- The FileReader and the FileWriter classes support Unicode-based file input and output
- The FileDescriptor class helps the host system to track files that are being accessed
- A FileInputStream obtains input bytes from a file in a file system. What files are available depends on the host environment.
- FileInputStream is meant for reading streams of raw bytes such as image data. For reading streams of characters, consider using FileReader.
- A FileOutputStream is an output stream for writing data to a File or to a FileDescriptor. Whether or not a file is available or may be created depends upon the underlying platform. Some platforms, in particular, allow a file to be opened for writing by only one FileOutputStream (or other file-writing object) at a time. In such situations the constructors in this class will fail if the file involved is already open.
- FileOutputStream is meant for writing streams of raw bytes such as image data. For writing streams of characters, consider using FileWriter.
- A ByteArrayInputStream contains an internal buffer that contains bytes that may be read from the stream. An internal counter keeps track of the next byte to be supplied by the read method.
- Closing a ByteArrayInputStream has no effect. The methods in this class can be called after the stream has been closed without generating an IOException.
public class ByteArrayInputStream extends InputStream
|Buffered I/O classes|
- Buffer means a temporary storage area.
- The time taken to retrieve data is less when data is temporarily stored in a buffer.
- These classes do not create a new stream but provide buffering functionality for existing stream.
- Filters operate on buffers, which are located between the program and the destination of the buffered stream.
Example 1: BufferedInputStream : this program will help you to download the text from remote url and tell you the time taken to download.
- Convenience class for reading character files. The constructors of this class assume that the default character encoding and the default byte-buffer size are appropriate. To specify these values yourself, construct an InputStreamReader on a FileInputStream.
- FileReader is meant for reading streams of characters. For reading streams of raw bytes, consider using a FileInputStream.
public class FileReader extends InputStreamReader
- Abstract class for writing filtered character streams. The abstract class FilterWriter itself provides default methods that pass all requests to the contained stream. Subclasses of FilterWriter should override some of these methods and may also provide additional methods and fields.
public class FileWriter extends OutputStreamWriter
|The RandomAccessFile Class|
- The RandomAccessFile class supports random accessing of files
- Can be used to read data from or write data to random locations within a file where as in all the other streams data is read and written as a continuous stream of information
- Implements the DataInput and DataOutput interfaces for performing input/output using the primitive data types
- Also supports permissions like read and write, and allows files to be accessed in read-only and read-write modes.
- Instances of this class support both reading and writing to a random access file.
- A random access file behaves like a large array of bytes stored in the file system.
- There is a kind of cursor, or index into the implied array, called the file pointer; input operations read bytes starting at the file pointer and advance the file pointer past the bytes read.
- RandomAccessFile(File file, String mode)
Creates a random access file stream to read from, and optionally to write to, the file specified by the File argument.
- RandomAccessFile(String name, String mode)
Creates a random access file stream to read from, and optionally to write to, a file with the specified name.
Has the following methods:
- void seek(long pos) – sets the file pointer to a particular location inside the file
- long getFilePointer() – returns the current location of the file pointer
- long length() – returns the length of the file, in bytes
- Serializability of a class is enabled by the class implementing the java.io.Serializable interface. Classes that do not implement this interface will not have any of their state serialized or deserialized.
- The capability of an object to exist beyond the execution of the program that created it is known as persistence.
- To make the file object persistent, the file object has to be saved, saving the file object is called serialization.
- JDK 1.2 provides the Serializable interface in the java.io package to support object serialization.
- The readObject() method of the ObjectInputStream class is used to read objects from the stream.
- The writeObject() method of the ObjectOutputStream class writes objects to a stream.
Example1 : Persistence of String Object
Example2 : Persistence of Customer Object