Java IO BufferedReader

In this section we will discuss about the BufferedReader class in Java.

Java IO BufferedReader

In this section we will discuss about the BufferedReader class in Java.

Java IO BufferedReader

Java IO BufferedReader

In this section we will discuss about the BufferedReader class in Java.

BufferedReader class in java.io package is used for read character input stream from a buffer. Size of buffer is default but, can be specified. In most of the purposes default size of buffer is used. BufferedReader should be wrapped around the Reader which read() method takes more time to read for example FileReader, InputStreamReader etc.

Constructor of BufferedReader

Constructor Name
Description
BufferedReader(Reader in) This constructor creates a BufferedReader with the default buffer size which contains the reference of Reader.
Syntax : public BufferedReader(Reader in)
BufferedReader(Reader in, int sz) This constructor creates a BufferedReader with the specified buffer size which contains the reference of Reader.
Syntax : public BufferedReader(Reader in, int sz)

Methods in BufferedReader

  • close() : This method is used to close the stream as well as release the resources associated with the stream.

    Syntax : public void close() throws IOException
     
  • mark(int readAheadLimit) : This method is used to mark the position in the stream.

    Syntax : public void mark(int readAheadLimit) throws IOException
     
  • markSupported() : Whether the mark() method is supported by this stream or not is specified by this method by giving a boolean value.s

    Syntax : public boolean markSupported()
     
  • read() : This method is used to read the characters from the stream.

    Syntax : public int read() throws IOException
     
  • read(char[] cbuf, int off, int len) : This method is used to read the characters of specified length from an array of characters, started reading from the specified offset 'off'.

    Syntax : public abstract int read(char[] cbuf, int off, int len) throws IOException
     
  • readLine() : This method is used to read a line from the input stream.

    Syntax : public String readLine() throws IOException
     
  • ready() : This method returns boolean value which specifies whether this stream is ready to be read characters or not.

    Syntax : public boolean ready() throws IOException
     
  • reset() : This method is used to reset the stream.

    Syntax : public void reset() throws IOException
     
  • skip(long n) : This method is used to skip over the characters.

    Syntax : public long skip(long n) throws IOExceptions

Example :

An example is being given here where I have used the java.io.BufferedReader class to read the stream from buffer. In this example I have used a BufferedReader with the default size which contains the reference of Reader as FileReader which provides the file to read from. The BufferedReader keeps the data into buffer.

Source Code

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class JavaBufferedReaderExample
{
  public static void main(String args[])
   {
      BufferedReader br = null;
     try
       {
           br = new BufferedReader(new FileReader("file.txt"));
          int c ;
          System.out.println();
          System.out.println("***** OUTPUT *****");
          System.out.println();
          while((c= br.read()) != -1)
            {
               System.out.print((char) c);
            }
          System.out.println();
       }
      catch(Exception ex)
       {
          System.out.println(ex);
       }
      finally
       {
          if(br != null)
            {
               try
                 {
                    br.close();
                 }
               catch(IOException ioe)
                 {
                     System.out.println(ioe);
                 }
            }
       }// end finally
   }// end main
}// end class

Output

When you will execute the above example you will get the output as follows :

Download Source Code