Programming Tutorials

BufferedReader sample program in Java

By: Charles in Java Tutorials on 2022-09-15  

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 a BufferedReader that reads text from the given Reader.
  • BufferedReader(Reader in, int sz) - creates a BufferedReader that reads text from the given Reader with the specified buffer size.
  • BufferedReader(InputStream in) - creates a BufferedReader that reads text from the given InputStream.
  • BufferedReader(InputStream in, int sz) - creates a BufferedReader that reads text from the given InputStream with the specified buffer size.





Add Comment

* Required information
1000

Comments

No comments yet. Be the first!

Most Viewed Articles (in Java )

Latest Articles (in Java)