Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the buffer size in BufferedReader?

What is the sense of buffer size in the constructor?

BufferedReader(Reader in, int size) 

As i have written the program:

import java.io.*; class bufferedReaderEx{     public static void main(String args[]){         InputStreamReader isr = null;         BufferedReader br = null;             try{                 isr = new InputStreamReader(System.in); //              System.out.println("Write data: "); //              int i = isr.read(); //              System.out.println("Data read is: " + i);                 //Thus the InputStreamReader is useful for reading the character from the stream                 System.out.println("Enter the data to be read by the bufferedReader: ");                 //here isr is containing the lnefeed already so this is needed to be flushed.                 br = new BufferedReader(isr, 2);                 String str = br.readLine();                 System.out.println("The data is : :" +  str);             }catch(IOException e){                 System.out.println("Can't read: " + e.getMessage());             }     } } 

Output:

Enter the data to be read by the bufferedReader: Hello world and hello world again The data is: Hello world and hello world again 

Then what does the buffer size means as i intended that it would be reading only two characters. but it was not that.

like image 256
codeomnitrix Avatar asked Jan 09 '11 11:01

codeomnitrix


People also ask

What is the default size of buffer in BufferedReader class?

By default, this will use a buffer of 8 KB. However, if we want to buffer smaller or larger blocks, we can use the BufferedReader(Reader, int) constructor: BufferedReader reader = new BufferedReader(new FileReader("src/main/resources/input.

How do I find BufferedReader size?

BufferedReader Buffer SizeYou provide the size as a constructor parameter, like this: int bufferSize = 8 * 1024; BufferedReader bufferedReader = new BufferedReader( new FileReader("c:\\data\\input-file. txt"), bufferSize ); This example sets the internal buffer to 8 KB.

What is buffer reader in java?

Java BufferedReader is a public Java class that reads text, using buffering to enable large reads at a time for efficiency, storing what is not needed immediately in memory for later use. Buffered readers are preferable for more demanding tasks, such as file and streamed readers.

What is the return type of read () method in BufferedReader class?

The read() method of BufferedReader class in Java is used to read a single character from the given buffered reader. This read() method reads one character at a time from the buffered stream and return it as an integer value.


2 Answers

BufferedReader buffers the input, just as the name says. This means that it reads from the input source into a buffer before passing it onto you. The buffer size here refers to the number of bytes it buffers.

Reading input from most sources is very slow. A buffer of just 2 bytes is going to hurt performance, as your program is very likely going to be waiting on input most of the time. With a buffer size of 2, a read of 100 bytes will result in reading 2 bytes from the in-memory buffer (very fast), filling the buffer (very slow), reading 2 bytes from the buffer (very fast), filling the buffer (very slow), etc - overall very slow. With a buffer size of 100, a read of 100 bytes will result in reading 100 bytes from the in-memory buffer (very fast) - overall very fast. This is assuming the buffer is contains the 100 bytes when reading though, which in a case like yours is a reasonable assumption to make.

Unless you know what you're doing, you should use the default buffer size which is quite large. One reason for a smaller buffer is when you are running on a limited-memory device, as the buffer consumes memory.

like image 200
moinudin Avatar answered Sep 24 '22 21:09

moinudin


http://www.docjar.com/html/api/java/io/BufferedReader.java.html

As per this java documentation, default buffer size is 8192 characters capacity. Line size is considered as 80 chars capacity.

8192 buffer size is sufficient for smaller file sizes. But again this is growable. if file contains more than 8192 characters, then fill method of bufferedreader will increase the buffer size before reading content from file. For bigger content files preferably set your own max size to buffer while creating buffered reader through constructor, so that you can avoid recreating memory and copying the old array into newly created array.

like image 24
user1923551 Avatar answered Sep 20 '22 21:09

user1923551