java.io.BufferedReader read()

Description :

This java tutorial shows how to use the read() method of BufferedReader class of java.io package. This method reads a single character and converted it into int as a method return. This BufferedReader method is widely used in reading characters from the InputStream as input on this class constructor.

Method Syntax :

public int read() throws IOException

Parameter Input :

DataType Parameter Description
N/A N/A N/A

Method Returns :

The Scanner read() method return the int equivalent of character read by the BufferedReader’s object. The int return range from 0 to 65535. If there is no character to be read because it already reaches the end of the stream, the method returns -1.

Compatibility Version :

Requires Java 1.1 and up

Exception :

IOException

– This method will throw IOException if the operation encounter an I/O exception.

Discussion :

The read() method of BufferedReader class is inherited from Reader class which is the parent of BufferedReader. This reader class is widely used usually in reading characters either from a file or from the console.

Java Code Example :

This java example source code demonstrates the use of read() method of BufferedReader class. Basically it just reads a character from the console input of the user and then prints the result in int data type and also in char data type. This is only a simple program that can be modified to a more complex logic, however for you to appreciate the use of this method, below is for beginner’s example of read() method.

Sample Output :

Running the read() method example source code of BufferedReader class will give you the following output.

java BufferedReader read method example

java BufferedReader read method example

Exception Scenario :

N/A

Similar Method :

  • N/A

Suggested Reading List :

References :