The Java Tutorials have been written for JDK 8.Java教程是为JDK 8编写的。Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
Most of the examples we've seen so far use unbuffered I/O.到目前为止,我们看到的大多数示例都使用无缓冲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.这会降低程序的效率,因为每个这样的请求通常会触发磁盘访问、网络活动或其他相对昂贵的操作。
To reduce this kind of overhead, the Java platform implements buffered I/O streams.为了减少这种开销,Java平台实现了缓冲I/O流。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.缓冲输入流从称为缓冲器的存储器区域读取数据;仅当缓冲区为空时才调用本机输入API。Similarly, buffered output streams write data to a buffer, and the native output API is called only when the buffer is full.类似地,缓冲输出流将数据写入缓冲区,并且仅当缓冲区已满时才调用本机输出API。
A program can convert an unbuffered stream into a buffered stream using the wrapping idiom we've used several times now, where the unbuffered stream object is passed to the constructor for a buffered stream class.程序可以使用我们现在多次使用的包装习惯用法将未缓冲流转换为缓冲流,其中未缓冲流对象被传递给缓冲流类的构造函数。Here's how you might modify the constructor invocations in the 下面是如何修改CopyCharacters
example to use buffered I/O:CopyCharacters
示例中的构造函数调用以使用缓冲I/O:
inputStream = new BufferedReader(new FileReader("xanadu.txt")); outputStream = new BufferedWriter(new FileWriter("characteroutput.txt"));
There are four buffered stream classes used to wrap unbuffered streams: 有四个缓冲流类用于包装非缓冲流:BufferedInputStream
and BufferedOutputStream
create buffered byte streams, while BufferedReader
and BufferedWriter
create buffered character streams.BufferedInputStream
和BufferedOutputStream
创建缓冲字节流,而BufferedReader
和BufferedWriter
创建缓冲字符流。
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
.PrintWriter
对象在每次调用println
或format
时刷新缓冲区。See Formatting for more on these methods.有关这些方法的详细信息,请参阅格式化。
To flush a stream manually, invoke its 要手动刷新流,请调用其flush
method.flush
方法。The flush
method is valid on any output stream, but has no effect unless the stream is buffered.flush
方法对任何输出流都有效,但除非对流进行缓冲,否则无效。