Java I/O Streams
Java I/O (Input and Output) is used to process the
input and produce the output.
Java uses the concept of a stream to make I/O operation fast. The
java.io package contains all the classes required for input and
output operations.
We can perform file handling in Java by Java I/O API.
Stream
A stream is a sequence of data. In Java, a stream is composed of bytes.
It's called a stream because it is like a stream of water that continues to
flow.
In Java, 3 streams are created for us automatically. All these streams are
attached with the console.
1) System.out: standard output stream
2) System.in: standard input stream
3) System.err: standard error stream
There are two kinds of Streams −
InPutStream − The InputStream is used to read data from a
source.
OutPutStream − The OutputStream is used for writing data
to a destination.
class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Here, the System.out is a type of output stream.
Similarly, there are input streams to take input.
Types of Streams
Depending upon the data a stream holds, it can be classified into:
Byte Stream
Character Stream
Byte Stream
Byte stream is used to read and write a single byte (8 bits) of data.
All byte stream classes are derived from base abstract classes
called InputStream and OutputStream .
Java InputStream Class
Java OutputStream Class
Character Stream
Character stream is used to read and write a single character of data.
All the character stream classes are derived from base abstract
classes Reader and Writer .
Java Reader Class
Java Writer Class
InputStream
Java application uses an input stream to read data from a source; it may
be a file, an array, peripheral device or socket.
OutputStream
Java application uses an output stream to write data to a destination; it
may be a file, an array, peripheral device or socket.
Java InputStream Class
The InputStream class of the java.io package is an abstract superclass that
represents an input stream of bytes.
Since InputStream is an abstract class, it is not useful by itself. However, its
subclasses can be used to read data.
Subclasses of InputStream
FileInputStream
ByteArrayInputStream
ObjectInputStream
Java OutputStream Class
The OutputStream class of the java.io package is an abstract superclass
that represents an output stream of bytes.
Since OutputStream is an abstract class, it is not useful by itself. However,
its subclasses can be used to write data.
Subclasses of OutputStream
FileOutputStream
ByteArrayOutputStream
ObjectOutputStream
Java Writer
It is an abstract class for writing to character streams. The methods that a
subclass must implement are write(char[], int, int), flush(), and close().
Subclasses of Writer
BufferedWriter
OutputStreamWriter
FileWriter
StringWriter
Java Reader Class
The Reader class of the java.io package is an abstract superclass that represents
a stream of characters.
Since Reader is an abstract class, it is not useful by itself. However, its subclasses
can be used to read data.
Subclasses of Reader
BufferedReader
InputStreamReader
FileReader
StringReader
Java BufferedWriter Class
Java BufferedWriter class is used to provide buffering for Writer instances.
It makes the performance fast. It inherits Writer class. The buffering
characters are used for providing the efficient writing of single arrays,
characters, and strings.
The BufferedWriter class of the java.io package can be used with other
writers to write data (in characters) more efficiently.
public class BufferedWriter extends Writer
Working of BufferedWriter
The BufferedWriter maintains an internal buffer of 8192 characters.
During the write operation, the characters are written to the internal buffer
instead of the disk. Once the buffer is filled or the writer is closed, the whole
characters in the buffer are written to the disk.
Hence, the number of communication to the disk is reduced. This is why
writing characters is faster using BufferedWriter .
Java BufferedReader
The BufferedReader class of the java.io package can be used with other readers
to read data (in characters) more efficiently.
It extends the abstract class Reader .
Working of BufferedReader
The BufferedReader maintains an internal buffer of 8192 characters.
During the read operation in BufferedReader , a chunk of characters is read from
the disk and stored in the internal buffer. And from the internal buffer characters
are read individually.
Hence, the number of communications to the disk is reduced. This is why reading
characters is faster using BufferedReader .
Reading data from console by InputStreamReader and BufferedReader
we are connecting the BufferedReader stream with
the InputStreamReader stream for reading the line by line data from the
keyboard.
public class BufferedReaderExample
{
public static void main(String args[])throws Exception
{
InputStreamReader r=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(r);
System.out.println("Enter your name");
String name=br.readLine();
System.out.println("Welcome "+name);
}
}
example of reading data from console until user writes stop
public class BufferedReaderExample
{
public static void main(String args[])throws Exception
{
InputStreamReader r=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(r);
String name="";
while(!name.equals("stop"))
{
System.out.println("Enter data: ");
name=br.readLine();
System.out.println("data is: "+name);
}
br.close();
r.close();
}
}