Java - BufferedReader Class



Introduction

The Java BufferedReader class reads text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines.Following are the important points about BufferedReader −

  • The buffer size may be specified, or the default size may be used.

  • Each read request made of a Reader causes a corresponding read request to be made of the underlying character or byte stream.

Class declaration

Following is the declaration for Java.io.BufferedReader class −

 public class BufferedReader extends Reader 

Field

Following are the fields for Java.io.BufferedReader class −

  • protected Object lock − This is the object used to synchronize operations on this stream.

Class constructors

Sr.No. Constructor & Description
1

BufferedReader(Reader in)

This creates a buffering character-input stream that uses a default-sized input buffer.

2

BufferedReader(Reader in, int sz)

This creates a buffering character-input stream that uses an input buffer of the specified size.

Class methods

Sr.No. Method & Description
1 void close()

This method closes the stream and releases any system resources associated with it.

2 void mark(int readAheadLimit)

This method marks the present position in the stream.

3 boolean markSupported()

This method tells whether this stream supports the mark() operation, which it does.

4 int read()

This method reads a single character.

5 int read(char[] cbuf, int off, int len)

This method reads characters into a portion of an array.

6 String readLine()

This method reads a line of text.

7 boolean ready()

This method tells whether this stream is ready to be read.

8 void reset()

This method resets the stream.

9 long skip(long n)

This method skips characters.

Methods inherited

This class inherits methods from the following classes −

  • Java.io.Reader
  • Java.io.Object

Example - Using mark() and reset() for Re-reading a Portion of the Input

The following example shows the usage of Java BufferedReader mark() and reset() methods.

BufferedReaderDemo.java

 package com.tutorialspoint; import java.io.BufferedReader; import java.io.IOException; import java.io.StringReader; public class BufferedReaderDemo { public static void main(String[] args) { // Input string String input = "Hello, World!\nThis is a BufferedReader example.\nHave a nice day!"; try (BufferedReader reader = new BufferedReader(new StringReader(input))) { // Read the first line System.out.println(reader.readLine()); // Mark the current position in the stream reader.mark(100); // Allows reading up to 100 characters before the mark is invalid // Read the next line System.out.println(reader.readLine()); // Reset the reader to the marked position reader.reset(); // Re-read the line after reset System.out.println(reader.readLine()); } catch (IOException e) { System.err.println("An error occurred: " + e.getMessage()); } } } 

Output

Let us compile and run the above program, this will produce the following result −

 Hello, World! This is a BufferedReader example. This is a BufferedReader example. 

Explanation

  • A BufferedReader is initialized with a StringReader that wraps the input string.

  • The first line is read using readLine() method.

  • The mark(100) method marks the current position, allowing up to 100 characters to be read before the mark becomes invalid.

  • After reading another line, the reset() method is called to return the reader to the marked position.

  • The previously read line is re-read after the reset.

Example - Reading and Printing Each Character

The following example shows the usage of Java BufferedReader read() method.

BufferedReaderDemo.java

 package com.tutorialspoint; import java.io.BufferedReader; import java.io.IOException; import java.io.StringReader; public class BufferedReaderDemo { public static void main(String[] args) { String input = "Hello, World!"; // Initialize BufferedReader with a StringReader try (BufferedReader reader = new BufferedReader(new StringReader(input))) { int character; System.out.println("Reading characters one by one:"); // Read each character until the end of the stream while ((character = reader.read()) != -1) { // Print the character (cast the integer to char) System.out.print((char) character); } } catch (IOException e) { System.err.println("An error occurred: " + e.getMessage()); } } } 

Output

Let us compile and run the above program, this will produce the following result −

 Reading characters one by one: Hello, World! 

Explanation

  • A BufferedReader is initialized with a StringReader containing the string "Hello, World!".

  • The read() method reads one character at a time from the stream.

  • The returned integer is cast to a char to display the actual character.

  • The loop continues until the end of the stream is reached (read() returns -1).

Example - Skipping Characters and Reading the Remaining Input

The following example shows the usage of BufferedReader skip(long n) method.

BufferedReaderDemo.java

 package com.tutorialspoint; import java.io.BufferedReader; import java.io.IOException; import java.io.StringReader; public class BufferedReaderDemo { public static void main(String[] args) { String input = "Hello, World! This is a BufferedReader skip() example."; // Initialize BufferedReader with a StringReader try (BufferedReader reader = new BufferedReader(new StringReader(input))) { // Skip the first 7 characters long skipped = reader.skip(7); System.out.println("Skipped characters: " + skipped); // Read and print the remaining input String remaining = reader.readLine(); System.out.println("Remaining input: " + remaining); } catch (IOException e) { System.err.println("An error occurred: " + e.getMessage()); } } } 

Output

Let us compile and run the above program, this will produce the following result −

 Skipped characters: 7 Remaining input: World! This is a BufferedReader skip() example. 

Explanation

  • The BufferedReader is initialized with a string containing a sentence.

  • The skip(7) method skips the first 7 characters in the stream ("Hello, ").

  • The program prints the number of characters actually skipped.

  • The readLine() method reads and prints the remaining input.

Advertisements