-3

BufferedReader.readLine() method vs FileReader.read(charArray) , if we pass a big size for charArray, performance of FileReader improves exponentially and looks like we can achieve what BufferedReader does, so why do we have BufferedReader and not just use FileReader with a big char array ?

How come BufferedReader is more efficient than FileReader when its just a decorator around FileReader (or any other Reader implementation) and depends upon FileReader(Reader instance) to read data from disk File ?

Does BufferedReader reduces number of I/O journeys to read data from disk as compared to FileReader ?

user3233451
  • 119
  • 2
  • 6
  • 17
  • The dupe I've marked is about writing, rather than reading, but it's the same explanation. – Andy Turner May 04 '18 at 09:40
  • 1
    *"Does `BufferedReader` reduces number of I/O journeys to read data from disk as compared to `FileReader`?"* - Yes. Basically, that is the point of buffering. The `BufferedReader` makes *big* reads against the `FileReader`, and the client makes *small* reads against the buffer – Stephen C May 04 '18 at 09:46
  • Can you please explain how it is same explanation ? For writing, it makes sense that BufferedWriter will collect the data and will not write every byte to disk until buffer fills. But for reading , BufferedReader cannot store the data until unless it is read from the disk byte by byte using underlying Reader instance. Please explain ? – user3233451 May 04 '18 at 09:51
  • @user3233451 Because it reads more than one byte at a time, as well as writing more than one byte at a time. See the Javadoc. – user207421 May 04 '18 at 09:53
  • A very good read i found on javaworld which answers this question or i would say gives a little insight to my original question. https://www.javaworld.com/article/2076241/build-ci-sdlc/tweak-your-io-performance-for-faster-runtime.html – user3233451 May 05 '18 at 07:56

1 Answers1

2

Because it is buffered. That cuts down the number of system calls by a factor of thousands.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • But it is buffered on the client side and not on disk, so how it will cut down the system calls ? For every byte read it has to read it using underlying FileReader, correct ? – user3233451 May 04 '18 at 09:47
  • @user3233451 *Because* it is buffered on the client side? and because 'that cuts down the number of system calls by a factor of thousands'? As I already said? – user207421 May 04 '18 at 09:47