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 read() method, without any parameters, returns an integer representation of the next available byte of input.
- The read(byte b[]) method reads the input as bytes into the array b and returns the actual number of bytes successfully read.
- The read(byte b[], int off, int len) reads up to len bytes into the array b, starting at b[off], returning the number of bytes successfully read.
- The read(char ebuf[], int off, int len) reads from the ebuf array of characters from the offset mentioned by off and up to the length mentioned by len.
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]);
}
}
- Save the file as ReadEg.java
- Compile the file using javac ReadEg.java
- Run the file using Java ReadEg
We have some more methods that can perform a similar action as above.
- The skip() Method: The number of bytes skipped is returned by this method after skipping over n bytes of the input.
- The available() Method: This method returns the number of bytes of input currently available for reading.
- The close() Method: This method closes the input source.
- The mark() Method: This method places a mark at the current point in the input stream until a certain number of bytes are read.
- The reset() Method: This method returns the input to the previously set mark.
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().
- The write() Method: The write(int b) method helps to write a single byte to an output stream.
- The write(byte b[]) assists in writing a complete array of bytes to an output stream.
- The write(byte b[], int off, int len) assists in writing len bytes from the array b, belonging at b[off].
- The following example uses both the write() and read() methods to read a string from the standard input and print it back to the same.
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);
}
}
- Save the file as WriteEg.java
- Compile the file using javac WriteEg.java
- Run the file using Java WriteEg
We have some more methods that can perform a similar action as above.
- The flush() Method: By using this method, any buffers in the output state are cleared.
- The close() Method: The output stream is closed using this method. Any additional writing attempts will result in an IOException.
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().