Saturday, November 16, 2024
Google search engine
HomeLanguagesJavaBufferedReader read() method in Java with Examples

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: It overrides the read() method of Reader class.

Parameters: This method does not accept any parameter.

Return value: This method returns the character that is read by this method in the form of an integer. If the buffered stream has ended and there is no character to be read then this method return -1.

Exceptions: This method throws IOException if an I/O error occurs.

Below program illustrates read() method in BufferedReader class in IO package:

Program: Assume the existence of the file “c:/demo.txt”.




// Java program to illustrate
// BufferedReader read() method
  
import java.io.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Read the stream 'demo.txt'
        // containing text "GEEKSFORGEEKS"
        FileReader fileReader
            = new FileReader(
                "c:/demo.txt");
  
        // Convert fileReader to
        // bufferedReader
        BufferedReader buffReader
            = new BufferedReader(
                fileReader);
  
        while (buffReader.ready()) {
            // Read and print characters one by one
            // by converting into character
            System.out.println("Char :"
                               + (char)buffReader.read());
        }
    }
}


Input:
Output:

2. The read(char[ ], int, int) method of BufferedReader class in Java is used to read characters in a part of a specific array.

General Contract:
The general contract of this read() method is as following:

  • It reads maximum possible characters by calling again and again the read() method of the main stream.
  • It continues till the reading of specified number of characters or till the ending of file or till ready() method has returned false.

Specified By: This method is specified by read() method of Reader class.

Syntax:

public int read(char[] cbuf,
                int offset,
                int length)
         throws IOException

Parameters: This method accepts three parameters:

  • cbuf – It represents the destination buffer.
  • offset – It represents the starting point to store the characters.
  • length – It represents the maximum number of characters that is to be read.

Return value: This method returns the number of characters that is read by this method. If the buffered stream has ended and there is no character to be read then this method return -1.

Exceptions: This method throws IOException if an I/O error occurs.

Below program illustrates read(char, int, int) method in BufferedReader class in IO package:

Program: Assume the existence of the file “c:/demo.txt”.




// Java program to illustrate
// BufferedReader read(char, int, int) method
  
import java.io.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Read the stream 'demo.txt'
        // containing text "GEEKSFORGEEKS"
        FileReader fileReader
            = new FileReader(
                "c:/demo.txt");
  
        // Convert fileReader to
        // bufferedReader
        BufferedReader buffReader
            = new BufferedReader(
                fileReader);
  
        // Create a character array
        char[] cbuf = new char[13];
  
        // Initialize and declare
        // offset and length
        int offset = 2;
        int length = 5;
  
        // Calling read() method
        // on buffer reader
        System.out.println(
            "Total number of characters read: "
            + buffReader.read(
                  cbuf, offset, length));
  
        // For each char in cbuf
        for (char c : cbuf) {
            if (c == (char)0)
                c = '-';
            System.out.print((char)c);
        }
    }
}


Input:
Output:

References:
https://docs.oracle.com/javase/10/docs/api/java/io/BufferedReader.html#read()
https://docs.oracle.com/javase/10/docs/api/java/io/BufferedReader.html#read(char%5B%5D, int, int)

RELATED ARTICLES

Most Popular

Recent Comments