Menu
Topics Index
...
`


Input/Output: Exploring java.io > Character Streams >
Siva Nookala - 06 Oct 2016
BufferedReader reads text from a character-input stream, to provide buffering for the efficient reading of characters, arrays and lines.
  • The buffer size may be specified, or the default size can be used.
  • Each read request causes a corresponding read request to character or byte stream. It is therefore advisable to wrap a BufferedReader around any Reader whose read() operations may be costly, such as Java FileReader and InputStreamReaders. For example,
    BufferedReader in = new BufferedReader(new FileReader("F:\\test.txt"));
    this will buffer the input from the test.txt file.

BufferedReader Constructors:
ConstructorDescription
BufferedReader(Reader in)Creates a buffering character-input stream that uses a default-sized input buffer.
BufferedReader(Reader in, int size)Creates a buffering character-input stream that uses an input buffer of the specified size.

BufferedReader Methods:
MethodsDescription
void close()Closes the stream and releases any system resources associated with it.
void mark(int readAheadLimit)Marks the present position in the stream.
boolean markSupported()Tells whether this stream supports the mark() operation, which it does.
int read()Reads a single character.
int read(char[] c, int from, int length)Reads characters into a portion of an array.
String readLine()Reads a line of text.
boolean ready()Tells whether this stream is ready to be read
void reset()Resets the stream.
long skip(long n)Skips characters.

Java.io.BufferedReader class inherits methods from the following classes:
  • Java.io.Reader
  • Java.io.Object

Program for read(), markSupported(), ready(), mark(), reset(), skip(), close().
Create a test.txt file containing "ABCDEFGHIJKLMNOPQRSTUVWXYZ" and save this in F drive.
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class TestA {
    public static void main(String[] args) {
        try {
            BufferedReader br = new BufferedReader(new FileReader("F:\\test.txt"));

            System.out.println((char) br.read()); // LINE A
            System.out.println((char) br.read());

            boolean support = false;
            support = br.markSupported(); // LINE B
            System.out.println("Buffered reader supports mark : " + support);

            boolean ready = false;
            ready = br.ready(); // LINE C
            System.out.println("Buffered reader is ready : " + ready);

            br.mark(2); // LINE D
            System.out.println("Mark() invoked");
            System.out.println((char) br.read());
            System.out.println((char) br.read());

            br.reset(); // LINE E
            System.out.println("reset() invoked");
            System.out.println((char) br.read());

            br.skip(2); // LINE F
            System.out.println("skipped 2 characters");
            System.out.println((char) br.read());

            br.close(); // LINE G
            br.read(); // LINE H
        } catch (IOException e) {
            System.out.println("You cannot read file because buffered Reader is closed.");
        }
    }
}
Output:
A
B
Buffered reader supports mark : true
Buffered reader is ready : true
Mark() invoked
C
D
reset() invoked
C
skipped 2 characters
F
You cannot read file because buffered Reader is closed.
Description: In this program,
at LINE A, reads first character in the text file and prints.
at LINE B, checks for the mark support returns true if applicable.
at LINE C, checks whether BufferReader is ready or not.
at LINE D & E, whenever reset method is called that points to that of mark position.
at LINE F, skips the number of characters as mentioned in braces.
at LINE G, closes the buffer reader.
at LINE H, exception error occurs as BufferReader is closed.


Program for readLine().
Create a test1.txt file containing "ABCDEF\nGHIJKL" and save this in F drive.
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class TestB {

    public static void main(String[] args) {

        try {
            BufferedReader br = new BufferedReader(new FileReader("F:\\test1.txt"));
            String sCurrentLine;

            while ((sCurrentLine = br.readLine()) != null) { // LINE I
                System.out.println(sCurrentLine);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
Output:
ABCDEF
GHIJKL
Description: In this program,
at LINE I, readLine method reads a line and then prints until it reaches the end of file.


Program for read(char[] c,int from,int length).
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;

public class TestC {
    public static void main(String[] args) throws Exception {
        try {
            InputStream is = new FileInputStream("F:\\test.txt");
            BufferedReader br = new BufferedReader(new InputStreamReader(is));

            char[] c = new char[is.available()];
            br.read(c, 2, 10); // LINE J

            for (char cc : c) {
                if (cc == (char) 0) {
                    cc = '*';
                }
                System.out.print(cc);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
Output:
**ABCDEFGHIJ**************
Description: In this program,
at LINE J, reads in a portion from 3rd position to 11th position of an array c.

Things to try:
  • Try to execute all the methods in eclipse.
  • Try to know the differences between all the methods.
  • I have used FileReader and InputStreamReader, try to do all methods individually using FileReader and InputStreamReader.

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App