GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Python Technologies
  • Databases
  • Computer Programming
  • Web Development
  • Java Technologies
  • Computer Science
  • Mobile Development
  • Big Data & Analytics
  • Microsoft Technologies
  • DevOps
  • Latest Technologies
  • Machine Learning
  • Digital Marketing
  • Software Quality

This view

  • Original page
  • New address

GeezerVison is showing www.tutorialspoint.com — Java - BufferedReader Class

Java.io Package Tutorial
  • Java.io - Home
  • Java.io - BufferedInputStream
  • Java.io - BufferedOutputStream
  • Java.io - BufferedReader
  • Java.io - BufferedWriter
  • Java.io - ByteArrayInputStream
  • Java.io - ByteArrayOutputStream
  • Java.io - CharArrayReader
  • Java.io - CharArrayWriter
  • Java.io - Console
  • Java.io - DataInputStream
  • Java.io - DataOutputStream
  • Java.io - File
  • Java.io - FileDescriptor
  • Java.io - FileInputStream
  • Java.io - FileOutputStream
  • Java.io - FilePermission
  • Java.io - FileReader
  • Java.io - FileWriter
  • Java.io - FilterInputStream
  • Java.io - FilterOutputStream
  • Java.io - FilterReader
  • Java.io - FilterWriter
  • Java.io - InputStream
  • Java.io - InputStreamReader
  • Java.io - LineNumberInputStream
  • Java.io - LineNumberReader
  • Java.io - ObjectInputStream
  • Java.io - ObjectInputStream.GetField
  • Java.io - ObjectOutputStream
  • io - ObjectOutputStream.PutField
  • Java.io - ObjectStreamClass
  • Java.io - ObjectStreamField
  • Java.io - OutputStream
  • Java.io - OutputStreamWriter
  • Java.io - PipedInputStream
  • Java.io - PipedOutputStream
  • Java.io - PipedReader
  • Java.io - PipedWriter
  • Java.io - PrintStream
  • Java.io - PrintWriter
  • Java.io - PushbackInputStream
  • Java.io - PushbackReader
  • Java.io - RandomAccessFile
  • Java.io - Reader
  • Java.io - SequenceInputStream
  • Java.io - SerializablePermission
  • Java.io - StreamTokenizer
  • Java.io - StringBufferInputStream
  • Java.io - StringReader
  • Java.io - StringWriter
  • Java.io - Writer
  • Java.io - Interfaces
  • Java.io - Exceptions
  • Java.io package Useful Resources
  • Java.io - Discussion
Selected Reading
  • UPSC IAS Exams Notes
  • Developer's Best Practices
  • Questions and Answers
  • Online Resume Builder
  • HR Interview Questions
  • Computer Glossary
  • Who is Who

Java - BufferedReader Class



Previous
Quiz
Next

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.

Tutorix - AI Tutor
Previous Quiz Next
Advertisements
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.