BufferedReader sample program in Java

By: Charles  

BufferedReader improves performance by buffering input. It has two constructors:

BufferedReader(Reader inputStream)
BufferedReader(Reader inputStream, int bufSize)

The first form creates a buffered character stream using a default buffer size. In the second, the size of the buffer is passed in bufSize. As is the case with the byte-oriented stream, buffering an input character stream also provides the foundation required to support moving backward in the stream within the available buffer. To support this, BufferedReader implements the mark() and reset() methods, and BufferedReader.markSupported( ) returns true.

The following example reworks the BufferedInputStream example, shown earlier, so that it uses a BufferedReader character stream rather than a buffered byte stream. As before, it uses mark( ) and reset( ) methods to parse a stream for the HTML entity reference for the copyright symbol. Such a reference begins with an ampersand (&) and ends with a semicolon (;) without any intervening whitespace. The sample input has two ampersands, to show the case where the reset( ) happens and where it does not. Output is the same as that shown earlier.

// Use buffered input.
import java.io.*;
class BufferedReaderDemo {
public static void main(String args[]) throws IOException {
String s = "This is a © copyright symbol " +
"but this is &copy not.\\n";
char buf[] = new char[s.length()];
s.getChars(0, s.length(), buf, 0);
CharArrayReader in = new CharArrayReader(buf);
BufferedReader f = new BufferedReader(in);
int c;
boolean marked = false;
while ((c = f.read()) != -1) {
switch(c) {
case '&':
if (!marked) {
marked = true;
} else {
marked = false;
case ';':
if (marked) {
marked = false;
} else
System.out.print((char) c);
case ' ':
if (marked) {
marked = false;
} else
System.out.print((char) c);
if (!marked)
System.out.print((char) c);

This tutorial is an extract from the "The Complete Reference Part 2 by Herbert Schildt".

Most Viewed Articles (in Java )

Latest Articles (in Java)

Comment on this tutorial

Subscribe to Tutorials

Related Tutorials

Archived Comments

1. ganda
View Tutorial          By: karla at 2008-11-13 17:35:07

2. i am looking for the samples in java programming u
View Tutorial          By: mary grace leron at 2010-02-18 01:50:50

3. Mary Grace Leon you're a moron. Use google you slo
View Tutorial          By: Peter at 2010-06-04 21:35:38

4. i am an internee in a software house working in ja
View Tutorial          By: sarmad at 2010-07-05 00:36:42

5. buffer reader??????????????????
View Tutorial          By: sunil bisht at 2010-10-21 02:05:38

6. i am Looking for the example the code of array usi
View Tutorial          By: bella at 2011-02-09 19:11:06

7. it's cool
View Tutorial          By: RInam Shah at 2011-06-20 02:48:20

8. can u access by a tutorial by using java script
View Tutorial          By: Benjtaz at 2011-07-11 06:22:16

9. How to read an array using BufferedReader?

View Tutorial          By: Dilnur Yuldashev at 2011-07-24 02:00:48

10. I want more example of bufferedReader.
View Tutorial          By: Romulado Bacor at 2011-11-25 01:18:57

11. it is a big help for us , as a IT student that kn
View Tutorial          By: nelson kent recites at 2012-01-19 08:56:26

12. Other Basic Java BufferedReader Programs

View Tutorial          By: Jeff Miranda at 2012-01-27 06:29:04

13. "THIS IS SAMPLE EXMPLE!!!!!!!!!!11111"
View Tutorial          By: david billa at 2012-08-02 06:23:43

14. I want simple programs using BuffuredReader for m
View Tutorial          By: Why ask my name? at 2013-06-01 04:56:00

15. I need simple programs... -_-
View Tutorial          By: Fuck Offff at 2016-02-15 14:08:23

16. what an example is this???????????????????????????
View Tutorial          By: Atul Anant at 2016-03-27 05:26:00

17. not getting the program
View Tutorial          By: Nikhil at 2017-08-29 16:05:14