BufferedReader is a class in Java that reads text from a character stream with efficiency (characters are buffered to avoid frequently reading from the underlying stream). Here's an example program that reads text from a file using BufferedReader:
import java.io.*;
public class BufferedReaderExample {
public static void main(String[] args) {
try (BufferedReader reader = new BufferedReader(new FileReader("example.txt"))) {
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
This program reads text from a file named example.txt using a BufferedReader. It reads each line of text from the file and prints it to the console.
The BufferedReader class has several constructors that can be used to create instances of it:
BufferedReader(Reader in)- creates aBufferedReaderthat reads text from the givenReader.BufferedReader(Reader in, int sz)- creates aBufferedReaderthat reads text from the givenReaderwith the specified buffer size.BufferedReader(InputStream in)- creates aBufferedReaderthat reads text from the givenInputStream.BufferedReader(InputStream in, int sz)- creates aBufferedReaderthat reads text from the givenInputStreamwith the specified buffer size.