Skip to Content
学习 Java,第 6 版
book

学习 Java,第 6 版

by Marc Loy, Patrick Niemeyer, Daniel Leuck
May 2025
Intermediate to advanced
552 pages
7h 22m
Chinese
O'Reilly Media, Inc.
Content preview from 学习 Java,第 6 版

第 10 章 文件输入和输出 文件输入和输出

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

将数据存储在文件中并在稍后进行检索的能力对于桌面应用程序和企业应用程序至关重要。在本章中,我们将介绍java.iojava.nio 包中一些最常用的类。这些包为基本输入和输出(I/O)提供了丰富的工具集,同时也提供了 Java 中所有文件和网络通信所依赖的框架。图 10-1显示了java.io 包的范围。

我们将首先查看java.io 中的流类,它们是基本InputStreamOutputStreamReaderWriter 类的子类。然后,我们将查看File 类,并讨论如何使用java.io 中的类读写文件。我们还将快速了解数据压缩和序列化。在此过程中,我们将介绍java.nio 包。新 "I/O 包(或 NIO)增加了为构建高性能服务量身定制的重要功能。NIO 的核心功能是处理缓冲区(存放数据,以便更有效地利用其他资源)和通道(有效地存放数据,并由其他人同样有效地接收)。在某些情况下,NIO 还提供了更好的 API,可用于替代java.io 的某些功能1

ljv6 1001
图 10-1. java.io 类的层次结构

溪流

Java 中的大多数 I/O 都基于流。从概念上讲,代表了数据流,一端是写入器,另一端是读取器。当您使用java.io 包执行终端输入和输出、读写文件或通过 Java 中的网络套接字进行通信时(第 13 章将详细介绍网络),您正在使用各种类型的流。当我们查看 NIO 包时,会发现一个类似的概念,叫做通道。两者的主要区别在于,流以字节或字符为中心,而通道以包含这些数据类型的 "缓冲区 "为中心。缓冲区通常是数据的快速临时存储区,可以更容易地优化吞吐量。两者的功能大致相同。让我们从流开始。下面是最常用的流类的快速概述:

InputStream,OutputStream

抽象 类,定义了读取或写入非结构化字节序列的基本功能。Java 中的所有其他字节流都建立在基本的InputStreamOutputStream 之上。

Reader,Writer

抽象类,定义了读写字符数据序列的基本功能,支持 Unicode。Java 中的所有其他字符流都建立在ReaderWriter 的基础之上。

InputStreamReader,OutputStreamWriter

根据特定字符编码方案(如 ASCII 或 Unicode)转换字节流和字符流的桥接类。(记住:在 Unicode 中,一个字符不一定是一个字节!)。

DataInputStream,DataOutputStream

专门的流过滤器,增加了以标准化格式读写多字节数据类型(如数字基元和String 对象)的能力。

ObjectInputStream,ObjectOutputStream

专门的流过滤器,能够写入整组序列化 Java 对象并对其进行重构。

BufferedInputStream,BufferedOutputStream,BufferedReaderBufferedWriter

专门的 流过滤器,增加了缓冲功能以提高效率。在现实世界的 I/O 中,几乎总是使用缓冲区。 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

《学习 Python》第 5 版

《学习 Python》第 5 版

Mark Lutz
CSS:权威指南,第 5 版

CSS:权威指南,第 5 版

Eric Meyer, Estelle Weyl
詳解 システム・パフォーマンス 第2版

詳解 システム・パフォーマンス 第2版

Brendan Gregg, 西脇 靖紘, 長尾 高弘

Publisher Resources

ISBN: 9798341656772