一、基础概念
Stream(流)是 C# 中一个非常重要的概念,它是用来处理输入/输出(I/O)的抽象概念,可以帮助我们更方便地操作文件或网络上的数据。在 C# 中,Stream 类定义在 System.IO 命名空间下,是所有其他流类的基类。
在下面的示例中,我们将展示如何创建文件,并向其中写入数据:
using System; using System.IO; class Program { static void Main(string[] args) { // 创建新文件 using (FileStream fs = File.Create("example.txt")) { // 往文件中写入字符串 string text = "Hello, C# Stream!"; byte[] bytes = System.Text.Encoding.UTF8.GetBytes(text); fs.Write(bytes, 0, bytes.Length); } } }
在以上代码中,我们使用 File.Create 方法创建一个名为 “example.txt” 的新文件,并使用 Write 方法将字符串写入文件中。在这个过程中,我们使用了 Stream 的派生类 FileStream,来完成文件的读写操作。
二、常用方法
Stream 类中定义了大量的方法,其中包括了读取、写入、搜索、定位以及转换等功能。下面是一些常用的方法:
1. Read 和 Write 方法
Read 和 Write 方法是 Stream 类中最基本的读写方法。其中,Read 方法的参数表示要读取的字节数,而 Write 方法的参数表示要写入的字节数。
using System; using System.IO; class Program { static void Main(string[] args) { // 打开已存在的文件 using (FileStream fs = File.OpenRead("example.txt")) { byte[] bytes = new byte[fs.Length]; fs.Read(bytes, 0, bytes.Length); string text = System.Text.Encoding.UTF8.GetString(bytes); Console.WriteLine(text); } } }
在以上代码中,我们使用 File.OpenRead 方法打开已存在的文件,然后使用 Read 方法将文件中的内容读取到字节数组中,并将其转换为字符串输出。
2. Seek 方法
Seek 方法用于将流中的位置设置为指定的字节偏移量,可用于在流中查找和定位特定的数据。
using System; using System.IO; class Program { static void Main(string[] args) { // 打开已存在的文件 using (FileStream fs = File.OpenRead("example.txt")) { // 查找指定字符串 string searchString = "C#"; byte[] searchBytes = System.Text.Encoding.UTF8.GetBytes(searchString); byte[] buffer = new byte[searchBytes.Length]; fs.Seek(0, SeekOrigin.Begin); while (fs.Read(buffer, 0, buffer.Length) > 0) { if (buffer.SequenceEqual(searchBytes)) { Console.WriteLine("Found the string \"{0}\".", searchString); break; } } } } }
在以上代码中,我们使用 File.OpenRead 方法打开已存在的文件,并使用 Seek 方法将文件的当前位置设置为字节偏移量 0。然后,使用 while 循环和 Read 方法,在文件中查找指定的字符串。
三、高级应用
除了基本的读写和定位功能外,Stream 类还可以进行更高级的操作。例如,可以使用流处理器(Stream Processor)来处理流中的数据;或者在基于网络的应用程序中使用网络流(NetworkStream)来进行通信。
1. 使用流处理器
在 C# 中,可以使用流处理器(Stream Processor)来操作流中的数据。流处理器是一种特殊的流,它可以修改流中的数据或将数据转换为其他格式。
using System; using System.IO; using System.IO.Compression; class Program { static void Main(string[] args) { // 将字符串压缩为 GZip string text = "Hello, C# Stream!"; byte[] bytes = System.Text.Encoding.UTF8.GetBytes(text); using (MemoryStream ms = new MemoryStream()) { using (GZipStream gzip = new GZipStream(ms, CompressionMode.Compress)) { gzip.Write(bytes, 0, bytes.Length); } Console.WriteLine("压缩后的字符串长度为 {0}。", ms.Length); } } }
在以上代码中,我们使用 GZipStream 流处理器将字符串压缩为 GZip 格式。在创建 GZipStream 实例时,需指定压缩模式为 CompressionMode.Compress。然后,将压缩后的数据写入 MemoryStream 中,并输出其长度。
2. 使用网络流
在基于网络的应用程序中,可以使用 NetworkStream 类来进行数据通信。NetworkStream 类是 Stream 类的派生类,它可以在网络上读取和写入数据。
using System; using System.Net.Sockets; class Program { static void Main(string[] args) { // 连接到网络服务端 TcpClient client = new TcpClient("localhost", 8080); using (NetworkStream stream = client.GetStream()) { // 发送数据 string text = "Hello, C# NetworkStream!"; byte[] bytes = System.Text.Encoding.UTF8.GetBytes(text); stream.Write(bytes, 0, bytes.Length); // 接收数据 byte[] buffer = new byte[1024]; int length = stream.Read(buffer, 0, buffer.Length); string response = System.Text.Encoding.UTF8.GetString(buffer, 0, length); Console.WriteLine(response); } client.Close(); } }
在以上代码中,我们使用 TcpClient 类连接到本地的服务端,并使用 GetStream 方法获取 NetworkStream 实例。接下来,在网络流中写入数据,并读取从服务端返回的响应数据。
原创文章,作者:YPLID,如若转载,请注明出处:https://www.506064.com/n/317550.html