Buffered Streams

本文探讨了Java中缓冲I/O流的概念及其重要性。通过使用缓冲区减少直接的磁盘或网络访问次数,可以显著提升程序效率。文章介绍了如何创建缓冲输入输出流,并讨论了自动冲洗缓冲区的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[url]http://java.sun.com/docs/books/tutorial/essential/io/buffers.html[/url]

Most of the examples we've seen so far use unbuffered I/O. This means each read or write request is handled directly by the underlying OS. This can make a program much less efficient, since each such request often triggers disk access, network activity, or some other operation that is relatively expensive.

前面的代码都是未缓冲IO实现的,这就表示每次操作都要触发磁盘访问或者网络,这些操作都很昂贵。

To reduce this kind of overhead, the Java platform implements buffered I/O streams. Buffered input streams read data from a memory area known as a buffer; the native input API is called only when the buffer is empty. Similarly, buffered output streams write data to a buffer, and the native output API is called only when the buffer is full.

为了减小开支,java实现了带缓冲的流。
输入流直接从内存的一块区域得到,也就是所说的buffer
input api只在缓冲区空了以后才被调用。类似的,输出直接写入缓冲区,只有当满的时候才调用output api。

unbuffered 未缓冲的流只需要包装一下就可以成为一个带缓冲的流了。

There are four buffered stream classes used to wrap unbuffered streams: [b]BufferedInputStream and BufferedOutputStream[/b] create buffered [b]byte streams[/b], while [b]BufferedReader and BufferedWriter[/b] create buffered [b]character streams[/b].

Flushing Buffered Streams

It often makes sense to write out a buffer at critical points, without waiting for it to fill. This is known as flushing the buffer.

写入缓冲临界点,还未满。所谓的冲洗缓冲区
Some buffered output classes support autoflush, specified by an optional constructor argument. When autoflush is enabled, certain key events cause the buffer to be flushed. For example, an autoflush PrintWriter object flushes the buffer on every invocation of println or format. See Formatting for more on these methods.
有些类支持自动冲洗缓存。或者在构造参数里可以指定。当自动冲洗开启,某些事件会触发缓冲被冲洗。例如 PrintWriter 调用 println 或 format 。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值