Last Updated : 28 May, 2020
The
read()method of
BufferedReaderclass in Java is of two types: 1. The
read()method of
BufferedReaderclass 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 IOExceptionOverrides:
It overrides the read() method of
Readerclass.
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
IOExceptionif 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".
Java
// Java program to illustrate
// BufferedReader read() method
import java.io.*;
public class GFG {
public static void main(String[] args)
{
// Read the stream 'demo.txt'
// containing text "GEEKSFORGEEKS"
FileReader fileReader
= new FileReader(
"c:/demo.txt");
// Convert fileReader to
// bufferedReader
BufferedReader buffReader
= new BufferedReader(
fileReader);
while (buffReader.ready()) {
// Read and print characters one by one
// by converting into character
System.out.println("Char :"
+ (char)buffReader.read());
}
}
}
Input: Output:
2. The
read(char[ ], int, int)method of
BufferedReaderclass 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:
This method is specified by read() method of
Readerclass.
Syntax:public int read(char[] cbuf, int offset, int length) throws IOExceptionParameters:
This method accepts three parameters:
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
IOExceptionif 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".
Java
// Java program to illustrate
// BufferedReader read(char, int, int) method
import java.io.*;
public class GFG {
public static void main(String[] args)
{
// Read the stream 'demo.txt'
// containing text "GEEKSFORGEEKS"
FileReader fileReader
= new FileReader(
"c:/demo.txt");
// Convert fileReader to
// bufferedReader
BufferedReader buffReader
= new BufferedReader(
fileReader);
// Create a character array
char[] cbuf = new char[13];
// Initialize and declare
// offset and length
int offset = 2;
int length = 5;
// Calling read() method
// on buffer reader
System.out.println(
"Total number of characters read: "
+ buffReader.read(
cbuf, offset, length));
// For each char in cbuf
for (char c : cbuf) {
if (c == (char)0)
c = '-';
System.out.print((char)c);
}
}
}
Input: 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)
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4