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) {
f.mark(32);
marked = true;
} else {
marked = false;
}
break;
case ';':
if (marked) {
marked = false;
System.out.print("(c)");
} else
System.out.print((char) c);
break;
case ' ':
if (marked) {
marked = false;
f.reset();
System.out.print("&");
} else
System.out.print((char) c);
break;
default:
if (!marked)
System.out.print((char) c);
break;
}
}
}
}

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




Archived Comments

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

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

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

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

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

6. Other Basic Java BufferedReader Programs
please click this link http://basicjavabufferedreade

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

7. it is a big help for us , as a IT student that knowing about the nature of java programming languag
View Tutorial          By: nelson kent recites at 2012-01-19 08:56:26

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

9. How to read an array using BufferedReader?
Nevertheless, the array is on one line not separat

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

10. can u access by a tutorial by using java script
how to apply its,uses,!!

View Tutorial          By: Benjtaz at 2011-07-11 06:22:16

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

12. i am Looking for the example the code of array using BufferedReader.in java.
View Tutorial          By: bella at 2011-02-09 19:11:06

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

14. i am an internee in a software house working in java language
till now confused to grasp over

View Tutorial          By: sarmad at 2010-07-05 00:36:42

15. Mary Grace Leon you're a moron. Use google you sloppy wet vag!
View Tutorial          By: Peter at 2010-06-04 21:35:38

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

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


Most Viewed Articles (in Java )

Latest Articles (in Java)

Comment on this tutorial