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发行说明。
Programs use byte streams to perform input and output of 8-bit bytes.程序使用字节流执行8位字节的输入和输出。All byte stream classes are descended from 所有字节流类都是InputStream
and OutputStream
.InputStream
和OutputStream
的后代。
There are many byte stream classes.有许多字节流类。To demonstrate how byte streams work, we'll focus on the file I/O byte streams, 为了演示字节流如何工作,我们将重点介绍文件I/O字节流、FileInputStream
and FileOutputStream
.FileInputStream
和FileOutputStream
。Other kinds of byte streams are used in much the same way; they differ mainly in the way they are constructed.其他类型的字节流的使用方式大致相同;它们的主要区别在于它们的构造方式。
We'll explore 我们将通过检查名为FileInputStream
and FileOutputStream
by examining an example program named CopyBytes
, which uses byte streams to copy xanadu.txt
, one byte at a time.CopyBytes
的示例程序来探索FileInputStream
和FileOutputStream
,该程序使用字节流一次复制一个字节来复制xanadu.txt
。
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class CopyBytes { public static void main(String[] args) throws IOException { FileInputStream in = null; FileOutputStream out = null; try { in = new FileInputStream("xanadu.txt"); out = new FileOutputStream("outagain.txt"); int c; while ((c = in.read()) != -1) { out.write(c); } } finally { if (in != null) { in.close(); } if (out != null) { out.close(); } } } }
CopyBytes
spends most of its time in a simple loop that reads the input stream and writes the output stream, one byte at a time, as shown in the following figure.CopyBytes
的大部分时间都花在一个简单的循环中,该循环读取输入流并写入输出流,每次一个字节,如下图所示。
Simple byte stream input and output.简单的字节流输入和输出。
Closing a stream when it's no longer needed is very important在不再需要时关闭流非常重要 so important that 非常重要,CopyBytes
uses a finally
block to guarantee that both streams will be closed even if an error occurs.CopyBytes
使用finally
块来保证即使发生错误,两个流也将关闭。This practice helps avoid serious resource leaks.这种做法有助于避免严重的资源泄漏。
One possible error is that 一个可能的错误是CopyBytes
was unable to open one or both files.CopyBytes
无法打开一个或两个文件。When that happens, the stream variable corresponding to the file never changes from its initial 发生这种情况时,与文件相对应的流变量永远不会从其初始null
value.null
值更改。That's why 这就是为什么CopyBytes
makes sure that each stream variable contains an object reference before invoking close
.CopyBytes
在调用close
之前确保每个流变量都包含一个对象引用。
CopyBytes
seems like a normal program, but it actually represents a kind of low-level I/O that you should avoid.CopyBytes
看起来像一个普通的程序,但实际上它代表了一种您应该避免的低级I/O。Since 由于xanadu.txt
contains character data, the best approach is to use character streams, as discussed in the next section.xanadu.txt
包含字符数据,最好的方法是使用字符流,如下一节所述。There are also streams for more complicated data types.还有用于更复杂数据类型的流。Byte streams should only be used for the most primitive I/O.字节流应仅用于最基本的I/O。
So why talk about byte streams? Because all other stream types are built on byte streams.那么为什么要谈论字节流呢?因为所有其他流类型都是基于字节流构建的。