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.
BufferedReader Constructors:
BufferedReader Methods:
Java.io.BufferedReader
class inherits methods from the following classes:
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
.