Introduction

A stream is a communication channel that connects an information source and a destination. Input streams, output streams, and readers/writers are the three categories under which streams can be discussed. They're all abstract classes. All the methods of these classes throw an IOException on error conditions. The methods that can be used with input streams, output streams, and readers/writers are listed below.

Methods of InputStream/Reader in Java

The following methods are available with the InputStream/Reader class read(), skip(), available(), close(), mark() & reset()

The read() method

This method waits for the user input and then reads bytes from the input source. The read() method comes in a number of variations.

The following examples illustrate the usage of the read() method.

Source Code

import java.io.*;
class ReadEg {
    public static void main(String args[]) throws IOException {
        int i;
        byte[] c = new byte[10];
        System.out.println("Enter a String of 10 Characters");
        for (i = 0; i < 10; i++)
            c[i] = (byte) System.in.read();
        System.out.println("The String entered is: ");
        for (i = 0; i < 10; i++)
            System.out.print((char) c[i]);
    }
}

Methods in Streams

We have some more methods that can perform a similar action as above.

Methods of OutputStream/Writer in Java

All the methods of this class return a void and, in case of errors, throws an IOException. Some of the methods of this class are write(), flush(), and close().

Source Code

import java.io.*;
class WriteEg {
    public static void main(String args[]) throws IOException {
        int i;
        byte[] c = new byte[10];
        System.out.println("Enter a String of 10 Characters");
        for (i = 0; i < 10; i++) {
            c[i] = (byte) System.in.read();
        }
        System.out.println("The Following is the string you have entered: ");
        System.out.write(c);
    }
}

Methods in Streams

We have some more methods that can perform a similar action as above.

Summary

Methods available with the InputStream/Reader class are read(), skip(), available(), close(), mark() and reset(). All methods of the OutputStream/Writer class return a void and, in case of errors, throws an IOException. Some of the methods of this class are write(), flush(), and close().