Documentation

The Java™ Tutorials
Hide TOC
Byte Streams字节流类
Trail: Essential Java Classes
Lesson: Basic I/O
Section: I/O Streams

Byte Streams字节流类

Programs use byte streams to perform input and output of 8-bit bytes.程序使用字节流执行8位字节的输入和输出。All byte stream classes are descended from InputStream and OutputStream.所有字节流类都是InputStreamOutputStream的后代。

There are many byte stream classes.有许多字节流类。To demonstrate how byte streams work, we'll focus on the file I/O byte streams, FileInputStream and FileOutputStream.为了演示字节流如何工作,我们将重点介绍文件I/O字节流、FileInputStreamFileOutputStreamOther kinds of byte streams are used in much the same way; they differ mainly in the way they are constructed.其他类型的字节流的使用方式大致相同;它们的主要区别在于它们的构造方式。

Using Byte Streams使用字节流

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的示例程序来探索FileInputStreamFileOutputStream,该程序使用字节流一次复制一个字节来复制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.

Simple byte stream input and output.简单的字节流输入和输出。

Always Close Streams始终要关闭流

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之前确保每个流变量都包含一个对象引用。

When Not to Use Byte Streams何时不使用字节流

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.那么为什么要谈论字节流呢?因为所有其他流类型都是基于字节流构建的。


Previous page: I/O Streams
Next page: Character Streams