Skip to Content
C# 12 核心要点
book

C# 12 核心要点

by Joseph Albahari
May 2025
Intermediate to advanced
1086 pages
14h 54m
Chinese
O'Reilly Media, Inc.
Content preview from C# 12 核心要点

第 15 章 流和 I/O 流和输入/输出

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

本章介绍 .NET 中输入和输出的基本类型,重点是以下主题:

  • .NET流架构及其如何为各种I/O类型的读写提供一致的编程接口

  • 操作磁盘上文件和目录的类

  • 用于压缩、命名管道和内存映射文件的专用流

本章主要介绍System.IO 命名空间中的类型,它是低级 I/O 功能的发源地。

流线型建筑

如图 15-1 所示,.NET 流架构以三个概念为中心:后备存储、装饰器和适配器。

后备存储是使输入和输出有用的端点,如文件或网络连接。准确地说,它是以下两个或其中之一:

  • 可按顺序读取字节的数据源

  • 可按顺序写入字节的目的地

Stream architecture
图 15-1. 数据流架构

不过,除非向程序员公开,否则后备存储是没有用的。Stream 是实现这一目的的标准 .NET 类;它提供了一套用于读取、写入和定位的标准方法。与数组不同的是,数组的所有后备数据都同时存在于内存中,而流则是以串行方式处理数据--每次一个字节或以大小可控的块为单位。因此,无论后备存储的大小如何,数据流都可以使用少量固定的内存。

溪流分为两类:

备份存储流
这些都是硬连接到特定类型的备份存储,如FileStreamNetworkStream
装饰流
这些数据流从另一个数据流馈入,以某种方式转换数据,如DeflateStreamCryptoStream

装饰流具有以下建筑优势:

  • 它们使后备存储流无需自己实现压缩和加密等功能。

  • 流在装饰时不会改变界面。

  • 您可以在运行时连接装饰器。

  • 您可以将装饰器串联在一起(例如,一个压缩器后接一个加密器)。

后备存储和装饰流都只处理字节。虽然这种方式灵活高效,但应用程序通常在更高层次(如文本或 XML)上工作。适配器通过将流封装在一个类中来弥补这一缺陷,该类具有根据特定格式键入的专门方法。例如,文本阅读器使用ReadLine 方法;XML 写入器使用WriteAttributes 方法。

备注

就像装饰器一样,适配器也会对流进行封装。不过,与装饰器不同的是,适配器本身并不是一个流;它通常会完全隐藏面向字节的方法。

简而言之,后备存储流提供原始数据;装饰流提供透明的二进制转换,如加密;适配器提供类型化方法,用于处理字符串和 XML 等高级类型。

图 15-1展示了它们之间的关联。要组成一个链,只需将一个对象传入另一个对象的构造函数即可。

使用流

抽象的Stream 类是所有流的基础。它定义了三种基本操作的方法和属性:读取写入查找,以及管理任务,如关闭、刷新和配置超时(见表 15-1)。

表 15-1. 数据流类成员
类别 成员
阅读 public abstract bool CanRead { get; }
public abstract int Read (byte[] buffer, int offset, int count)
public virtual int ReadByte();
写作 public abstract bool CanWrite { get; }
public abstract void Write (byte[] buffer, ...
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

Programming C# 12

Programming C# 12

Ian Griffiths
C# 12 in a Nutshell

C# 12 in a Nutshell

Joseph Albahari
C# 6 for Programmers, Sixth Edition

C# 6 for Programmers, Sixth Edition

Paul Deitel, Harvey Deitel
Head First C#, 4th Edition

Head First C#, 4th Edition

Andrew Stellman, Jennifer Greene

Publisher Resources

ISBN: 9798341657038