(Translated by https://www.hiragana.jp/)
Java.io.BufferedReader Class in Java - GeeksforGeeks
Open In App

Java.io.BufferedReader Class in Java

Last Updated : 03 May, 2022
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Reads text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines. The buffer size may be specified, or the default size may be used. The default is large enough for most purposes. In general, each read request made by a Reader causes a corresponding read request to be made of the underlying character or byte stream. It is therefore advisable to wrap a BufferedReader around any Reader whose read() operations may be costly, such as FileReaders and InputStreamReaders. Programs that use DataInputStreams for textual input can be localized by replacing each DataInputStream with an appropriate BufferedReader.

Constructors of BufferedReader Class 

Constructor Action Performed
BufferedReader(Reader in) Creates a buffering character-input stream that uses a default-sized input buffer
BufferedReader(Reader in, int sz) Creates a buffering character-input stream that uses an input buffer of the specified size.

Methods of BufferedReader Class

Method Name Action 
close() Closes the stream and releases any system resources associated with it.Once the stream has been closed, further read(), ready(), mark(), reset(), or skip() invocations will throw an IOException. Closing a previously closed stream has no effect.
mark() Marks the present position in the stream. Subsequent calls to reset() will attempt to reposition the stream to this point.
markSupported() Tells whether this stream supports the mark() operation, which it does.
read() Reads a single character.
read(char[] cbuf, int off, int len) Reads characters into a portion of an array. This method implements the general contract of the corresponding read method of the Reader class. As an additional convenience, it attempts to read as many characters as possible by repeatedly invoking the read method of the underlying stream.
readLine() Reads a line of text. A line is considered to be terminated by any one of a line feed (‘\n’), a carriage return (‘\r’), or a carriage return followed immediately by a line feed.
ready() Tells whether this stream is ready to be read.
reset() Resets the stream to the most recent mark.
skip(long) Skips characters.

Implementation: The content inside the file is as follows:

This is first line
this is second line

Example

Java




// Java Program to Illustrate BufferedReader Class
// Via Its Methods
  
// Importing required classes
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
  
// Class
class GFG {
  
    // Main driver method
    public static void main(String[] args)
        throws IOException
    {
  
        // Creating object of FileReader and BufferedReader
        // class
        FileReader fr = new FileReader("file.txt");
        BufferedReader br = new BufferedReader(fr);
  
        char c[] = new char[20];
  
        // Illustrating markSupported() method
        if (br.markSupported()) {
  
            // Print statement
            System.out.println(
                "mark() method is supported");
  
            // Illustrating mark method
            br.mark(100);
        }
  
        // File Contents is as follows:
        // This is first line
        // this is second line
  
        // Skipping 8 characters
        br.skip(8);
  
        // Illustrating ready() method
        if (br.ready()) {
  
            // Illustrating readLine() method
            System.out.println(br.readLine());
  
            // Illustrating read(char c[],int off,int len)
            br.read(c);
  
            for (int i = 0; i < 20; i++) {
                System.out.print(c[i]);
            }
  
            System.out.println();
  
            // Illustrating reset() method
            br.reset();
            for (int i = 0; i < 8; i++) {
  
                // Illustrating read() method
                System.out.print((char)br.read());
            }
        }
    }
}


Output:

mark() method is supported
first line
this is second line
This is 



Previous Article
Next Article

Similar Reads

Why BufferedReader class takes less time for I/O operation than Scanner class in java
In Java, both BufferReader and Scanner classes can be used for the reading inputs, but they differ in the performance due to their underlying implementations. The BufferReader class can be generally faster than the Scanner class because of the way it handles the input and parsing. This article will guide these difference, provide the detailed expla
3 min read
BufferedReader Class lines() method in Java with Examples
BufferedReader.lines() is the method of the Java Buffered Reader Class in the Java Library which returns lines in terms of Stream and from this Buffered Reader class. With the help of the stream, there are a lot of methods that mimic the output according to our needs. Syntax: BufferedReader.lines() : Stream&lt;String&gt; Parameters: This method doe
2 min read
Difference Between Scanner and BufferedReader Class in Java
In Java, Scanner and BufferedReader class are sources that serve as ways of reading inputs. Scanner class is a simple text scanner that can parse primitive types and strings. It internally uses regular expressions to read different types while on the other hand BufferedReader class reads text from a character-input stream, buffering characters so a
3 min read
BufferedReader reset() method in Java with Examples
The reset() method of BufferedReader class in Java is used to fix or mark the position at the last marked position so that the same byte can be read again. Syntax: public void reset() throws IOException Overrides: It overrides the reset() method of Reader class. Parameters: The method does not accept any parameter. Return value: The method does not
3 min read
BufferedReader mark() method in Java with Examples
The mark() method of BufferedReader class in Java is used to mark the current position in the buffer reader stream. The reset() method of the same BufferedReader class is also called subsequently, after the mark() method is called. The reset() method fixes the position at the last marked position so that same byte can be read again. Syntax: public
3 min read
BufferedReader markSupported() method in Java with Examples
The markSupported() method of BufferedReader class in Java is used to verify whether the stream supports the mark() method or not. It returns the boolean value true if the stream supports mark() otherwise it returns false. Syntax: public boolean markSupported() Overrides: It overrides the markSupported() method of the Reader class. Parameters: This
2 min read
BufferedReader close() method in Java with Examples
The close() method of BufferedReader class in Java is used to close the stream and release all the system resources associated with the stream operations. Syntax: public void close() throws IOException Parameters: This method does not accept any parameter. Return value: This method does not return any value. Exception: This method throws IOExceptio
2 min read
BufferedReader ready() method in Java with Examples
The ready() method of BufferedReader class in Java is used to verify whether the buffer stream is ready to be read or not. A buffer stream is said to be ready in two cases either the buffer is not empty or the main stream is ready. Syntax: public boolean ready() throws IOException Overrides: This method overrides ready() method of Reader class. Par
2 min read
BufferedReader read() method in Java with Examples
The read() method of BufferedReader class in Java is of two types: 1. The read() method of BufferedReader class 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 IOException Overrides
3 min read
BufferedReader readLine() method in Java with Examples
The readLine() method of BufferedReader class in Java is used to read one line text at a time. The end of a line is to be understood by '\n' or '\r' or EOF. Syntax: public String readLine() throws IOException Parameters: This method does not accept any parameter. Return value: This method returns the String that is read by this method and excludes
2 min read
BufferedReader skip(long) method in Java with Examples
The skip() method of BufferedReader class in Java is used to skip characters in the stream. The number of characters to be skipped is passed as parameter in this method. Syntax: public long skip(long n) throws IOException Overrides: This method overrides skip() method of Reader class. Parameters: This method accepts one parameter i.e. n which repre
2 min read
Why Does BufferedReader Throw IOException in Java?
IOException is a type of checked exception which occurs during input/output operation. BufferedReader is used to read data from a file, input stream, database, etc. Below is the simplified steps of how a file is read using a BufferedReader in java. In RAM a buffered reader object is created.Some lines of a file are copied from secondary memory ( or
2 min read
Difference Between BufferedReader and FileReader in Java
BufferedReader and FileReader both classes are used to read data from a given character stream. Both of them have some pros and some cons. In this article, we will discuss the differences between them. Though the most important difference is in the way they work, but we will discuss the other details also. What is a Buffer? A buffer is a small port
9 min read
Java.lang.Class class in Java | Set 1
Java provides a class with name Class in java.lang package. Instances of the class Class represent classes and interfaces in a running Java application. The primitive Java types (boolean, byte, char, short, int, long, float, and double), and the keyword void are also represented as Class objects. It has no public constructor. Class objects are cons
15+ min read
Java.lang.Class class in Java | Set 2
Java.lang.Class class in Java | Set 1 More methods: 1. int getModifiers() : This method returns the Java language modifiers for this class or interface, encoded in an integer. The modifiers consist of the Java Virtual Machine's constants for public, protected, private, final, static, abstract and interface. These modifiers are already decoded in Mo
15+ min read
Java.util.TimeZone Class (Set-2) | Example On TimeZone Class
TimeZone class (the methods of this class was discussed in this article Java.util.TimeZone Class | Set 1) can be used in many cases like using TimeZone class we can get the time difference in term of hour and minute between two places.Problem : How we can get time difference of time in terms of hours and minutes between two places of Earth?Solution
5 min read
Implement Pair Class with Unit Class in Java using JavaTuples
To implement a Pair class with a Unit class in Java using JavaTuples, you can use the Pair class provided by the library and create a new Unit class that extends the Unit class provided by the library. Here is an example implementation: Here is an example Java code that uses the MyPair class with MyUnit and displays the output: Java Code import org
4 min read
Implement Triplet Class with Pair Class in Java using JavaTuples
Following are the ways to implement Triplet Class with Pair Class Using direct values import java.util.*; import org.javatuples.*; class GfG { public static void main(String[] args) { // create Pair Pair&lt;Integer, String&gt; pair = new Pair&lt;Integer, String&gt;( Integer.valueOf(1), &quot;GeeksforGeeks&quot;); // Print the Pair System.out.printl
2 min read
Implement Quintet Class with Quartet Class in Java using JavaTuples
Following are the ways to implement Quintet Class with Quartet Class Using direct values import java.util.*; import org.javatuples.*; class GfG { public static void main(String[] args) { // create Quartet Quartet&lt;String, String, String, String&gt; quartet = new Quartet&lt;String, String, String, String&gt;( &quot;Quartet&quot;, &quot;Triplet
4 min read
Implement Quartet Class with Triplet Class in Java using JavaTuples
Following are the ways to implement Quartet Class with Triplet Class Using direct values import java.util.*; import org.javatuples.*; class GfG { public static void main(String[] args) { // create Triplet Triplet&lt;String, String, String&gt; triplet = new Triplet&lt;String, String, String&gt;( &quot;Triplet 1&quot;, &quot;1&quot;, &quot;GeeksforGe
3 min read
Implement Octet Class from Septet Class in Java using JavaTuples
Prerequisite: Octet Class, Septet Class Below are the methods to implement a Octet Class using Septet Class in Java: Using direct values // Java program to illustrate // implementing Octet Class // from Septet Class // using direct values import java.util.*; import org.javatuples.*; class GfG { public static void main(String[] args) { // Create Sep
6 min read
Implement Ennead Class from Octet Class in Java using JavaTuples
Prerequisite: Ennead Class, Octet Class Below are the methods to implement a Ennead Class using Octet Class in Java: Using direct values // Java program to illustrate // implementing Ennead Class // from Octet Class // using direct values import java.util.*; import org.javatuples.*; class GfG { public static void main(String[] args) { // Create Oct
7 min read
Implement Sextet Class from Quintet Class in Java using JavaTuples
Prerequisite: Sextet Class, Quintet Class Below are the methods to implement a Sextet Class using Quintet Class in Java: Using direct values // Java program to illustrate // implementing Sextet Class // from Quintet Class using // direct values import java.util.*; import org.javatuples.*; class GfG { public static void main(String[] args) { // crea
5 min read
Implement Septet Class from Sextet Class in Java using JavaTuples
Prerequisite: Septet Class, Sextet Class Below are the methods to implement a Septet Class using Sextet Class in Java: Using direct values // Java program to illustrate // implementing Septet Class // from Sextet Class // using direct values import java.util.*; import org.javatuples.*; class GfG { public static void main(String[] args) { // Create
6 min read
Implement Decade Class from Ennead Class in Java using JavaTuples
Prerequisite: Decade Class, Ennead Class Below are the methods to implement a Decade Class using Ennead Class in Java: Using direct values // Java program to illustrate // implementing Decade Class // from Ennead Class // using direct values import java.util.*; import org.javatuples.*; class GfG { public static void main(String[] args) { // Create
8 min read
Difference between Abstract Class and Concrete Class in Java
Abstract Class: An abstract class is a type of class in Java that is declared by the abstract keyword. An abstract class cannot be instantiated directly, i.e. the object of such class cannot be created directly using the new keyword. An abstract class can be instantiated either by a concrete subclass or by defining all the abstract method along wit
5 min read
Java - Inner Class vs Sub Class
Inner Class In Java, one can define a new class inside any other class. Such classes are known as Inner class. It is a non-static class, hence, it cannot define any static members in itself. Every instance has access to instance members of containing class. It is of three types: Nested Inner ClassMethod Local Inner ClassAnonymous Inner Class Genera
3 min read
Java I/O Operation - Wrapper Class vs Primitive Class Variables
It is better to use the Primitive Class variable for the I/O operation unless there is a necessity of using the Wrapper Class. In this article, we can discuss briefly both wrapper class and primitive data type. A primitive data type focuses on variable values, without any additional methods.The Default value of Primitive class variables are given b
3 min read
Using predefined class name as Class or Variable name in Java
In Java, you can use any valid identifier as a class or variable name. However, it is not recommended to use a predefined class name as a class or variable name in Java. The reason is that when you use a predefined class name as a class or variable name, you can potentially create confusion and make your code harder to read and understand. It may a
5 min read
Does JVM create object of Main class (the class with main())?
Consider following program. class Main { public static void main(String args[]) { System.out.println(&quot;Hello&quot;); } } Output: Hello Does JVM create an object of class Main? The answer is "No". We have studied that the reason for main() static in Java is to make sure that the main() can be called without any instance. To justify the same, we
1 min read
Article Tags :
Practice Tags :