Buffered vs non buffered, which one to use?
Asked Answered
M

5

12

I am sorry if this is a duplicate but I was not able to find a definitive answer to what is the best practice for each type.

I would like to know what the appropriate conditions are that define when to use BufferedReader vs FileReader or BufferedInput/OutputStream vs FileInput/OutputStream? Is there a formula of sorts that will always tell you what is appropriate?

Should I just always used buffered?

Thanks

Moa answered 6/7, 2009 at 18:41 Comment(4)
What do you mean by "definitive"?Shillelagh
S.Lott: I think he's just looking for when you should use each one.Epicureanism
I guess definitive == what is the best practice for each typeMoa
@stackMe: Please update your question to clarify it. Don't comment on your own questions.Shillelagh
U
15

Use a buffer if the stream is going to have lots of small access. Use unbuffered if you are going to have relatively few, relatively large accesses.

Uninspired answered 6/7, 2009 at 18:43 Comment(0)
S
4

The only time you should use unbuffered I/O is when the delay and aggregation imposed by buffering is inappropriate to your application.

Shel answered 6/7, 2009 at 18:43 Comment(1)
What about when you're buffering things yourself between an InputStream & OutputStream (or vice versa) with a byte[]. Would you wrap a Buffered* around it?Packston
S
3

" Is there a formula of sorts that will always tell you what is appropriate?"

If there was, it would already be in the libraries and would not be a design decision that you would have to make.

Since there's no pat answer, you have to make the design decision, you have to actually think about it.

Or, you can try both options and see which is "better" based on your unique problem and your unique criteria.

Most standard I/O libraries are buffered. That's a hint that most I/O benefits from buffering. But not all. Games, for instance, need unbuffered access to the game controls.

Shillelagh answered 6/7, 2009 at 18:46 Comment(0)
C
2

Keep in mind also that the BufferedReader provides you with a convenience readLine() method that allows you to read your content one line at a time.

Clemons answered 6/7, 2009 at 18:47 Comment(0)
M
1

I suggest you use Buffered* if this makes your application go faster, otherwise I wouldn't bother with it. i.e. try it with realistic data for see whether it helps.

Moser answered 7/7, 2009 at 5:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.