Buffered vs non buffered, which one to use?

2019-03-15 13:27发布

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

5条回答
迷人小祖宗
2楼-- · 2019-03-15 13:36

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.

查看更多
劳资没心,怎么记你
3楼-- · 2019-03-15 13:37

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.

查看更多
来,给爷笑一个
4楼-- · 2019-03-15 13:43

" 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.

查看更多
萌系小妹纸
5楼-- · 2019-03-15 14:01

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.

查看更多
ゆ 、 Hurt°
6楼-- · 2019-03-15 14:02

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

查看更多
登录 后发表回答