Friday, 26 April 2019

What is the difference between Bufferreader and InputstreamReader in Java? tccicomputercoaching.com

BufferedReader and InputStreamReader are both Input Function in Java File.

BufferedReader reads a couple of characters from the specified stream and stores it in a buffer. This makes input faster.

InputStreamReader reads only one character from specified stream and remaining characters still remain in the stream.



Example:

1. class NewClass{
2. public static void main(String args[]) throws InterruptedException, IOException{
3.
4. BufferedReader isr = new BufferedReader( new InputStreamReader (System.in));
5.
6. Scanner sc = new Scanner(System.in);
7.
8. System.out.println("B.R. - "+(char )isr.read());
9. System.out.println("Scanner - " + sc.nextLine());
10. }
11. }

When the isr.read() statement is executed, I entered the input ”hello” and the character “h” of “hello” is printed on the screen. If this was InputStreamReader then the remaining characters “ello” would have remained in the System.in stream and the sc.nextLine() would have printed them. But in this case it doesn’t happens because the BufferedReader reads all of the “hello” characters from the System. in stream and stores them in its own personal buffer and thus the System. In stream remains empty when sc.nextLine() is executed.

For more information about Java, C++, Vacation Batch at TCCI , Python at TCCI

Call us @ 9825618292

Visit us @ www.tccicomputercoaching.com

No comments:

Post a Comment