r/javahelp • u/Informal_Fly7903 • Nov 18 '25
Codeless Why do we need BufferedReader class in Java?
Why do we need the BufferedReader class in Java IF the InputStreamReader class already has buffering? If we look in the source code of InputStreamReader we can see that it delegates its calls to the StreamDecoder class - this class HAS buffering (of 8KB). So why would we still use BufferedReader? Backwards compatibility reasons? I'm so confused!
EDIT: Also I've checked how long the reading 'character by character' of a very large text file (80MB) will take using both of them. The difference is of 0.5 seconds (in total it took about 1.5 secs to 2 secs). No idea why.
•
u/AutoModerator Nov 18 '25
Please ensure that:
You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.
Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar
If any of the above points is not met, your post can and will be removed without further warning.
Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.
Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.
Code blocks look like this:
You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.
If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.
To potential helpers
Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
21
u/morhp Professional Developer Nov 18 '25
The
BufferedReaderclass has a usefulreadLine()method. At least it was very useful years ago when you didn't had nice methods likeFiles.readAllLines()orFiles.lines(). Plus it's just parallel to theBufferedInputStream. And it may be useful for adding buffering to your ownReaderimplementation.Also, these classes are from Java 1.1, they're pretty old and don't necessarily have the greatest design. (Although I'd say they're much less worse than some other candidates, like
VectororDate)6
u/lepapulematoleguau Nov 18 '25
You can wrap any Reader instance in a BufferedReader, not only an InputStreamReader.
1
u/hrm Nov 18 '25
Well, darn it. You now confused me as well. The javadoc for InputStreamReader does suggest the BufferedReader for best performance. I.e. the explanation "can be good for other readers" is sound, but does not explain why it is suggested in the javadoc for an already buffered reader. And of course the existence of Files.newBufferedReader/Writer suggests that the buffered reader is what should be used.
One difference is that the buffer for StreamDecoder is 8k bytes while the buffer for BufferedReader is 8k chars, so the latter is bigger. Though that does not seem like something that should make a big difference.
When it comes to measuring performance, that is quite hard to do correctly, with interference from other processes, OS caching, JIT compilation and all that other jazz that can confound things so I'd be quite suspicious of your results without any deeper explanation of what you are doing.
2
u/ashdgjklashgjkdsahkj Nov 19 '25 edited 5d ago
The original post content no longer exists here. The author used Redact to remove it, exercising their right to control their data & privacy.
Ask pumpkin future seed liquid mellow act offbeat steer unite