BufferedReader read() method in Java with Examples
Last Updated :
28 May, 2020
The read() method of BufferedReader class in Java is of two types:
1. The read() method of BufferedReader class in Java is used to read a single character from the given buffered reader. This read() method reads one character at a time from the buffered stream and return it as an integer value.
Syntax:
public int read()
throws IOException
Overrides: It overrides the read() method of Reader class.
Parameters: This method does not accept any parameter.
Return value: This method returns the character that is read by this method in the form of an integer. If the buffered stream has ended and there is no character to be read then this method return -1.
Exceptions: This method throws IOException if an I/O error occurs.
Below program illustrates read() method in BufferedReader class in IO package:
Program: Assume the existence of the file “c:/demo.txt”.
import java.io.*;
public class GFG {
public static void main(String[] args)
{
FileReader fileReader
= new FileReader(
"c:/demo.txt" );
BufferedReader buffReader
= new BufferedReader(
fileReader);
while (buffReader.ready()) {
System.out.println( "Char :"
+ ( char )buffReader.read());
}
}
}
|
Output:
2. The read(char[ ], int, int) method of BufferedReader class in Java is used to read characters in a part of a specific array.
General Contract:
The general contract of this read() method is as following:
- It reads maximum possible characters by calling again and again the read() method of the main stream.
- It continues till the reading of specified number of characters or till the ending of file or till ready() method has returned false.
Specified By: This method is specified by read() method of Reader class.
Syntax:
public int read(char[] cbuf,
int offset,
int length)
throws IOException
Parameters: This method accepts three parameters:
- cbuf – It represents the destination buffer.
- offset – It represents the starting point to store the characters.
- length – It represents the maximum number of characters that is to be read.
Return value: This method returns the number of characters that is read by this method. If the buffered stream has ended and there is no character to be read then this method return -1.
Exceptions: This method throws IOException if an I/O error occurs.
Below program illustrates read(char, int, int) method in BufferedReader class in IO package:
Program: Assume the existence of the file “c:/demo.txt”.
import java.io.*;
public class GFG {
public static void main(String[] args)
{
FileReader fileReader
= new FileReader(
"c:/demo.txt" );
BufferedReader buffReader
= new BufferedReader(
fileReader);
char [] cbuf = new char [ 13 ];
int offset = 2 ;
int length = 5 ;
System.out.println(
"Total number of characters read: "
+ buffReader.read(
cbuf, offset, length));
for ( char c : cbuf) {
if (c == ( char ) 0 )
c = '-' ;
System.out.print(( char )c);
}
}
}
|
Output:
References:
https://docs.oracle.com/javase/10/docs/api/java/io/BufferedReader.html#read()
https://docs.oracle.com/javase/10/docs/api/java/io/BufferedReader.html#read(char%5B%5D, int, int)