一、基礎概念
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/zh-tw/n/317550.html