撰寫資料流
備份存放區是一種儲存媒體,例如磁碟或記憶體。 每個不同的備份存放區都會實作自己的資料流,作為 Stream 類別的實作。
每個資料流類型都會在其指定的備份存放區中讀取和寫入位元組。 連線到備份存放區的資料流稱為基底資料流。 基底資料流具有建構函式,其中包含將資料流連線至備份存放區所需的參數。 例如,FileStream 的建構函式會指定路徑參數,此參數可指定處理程序共用檔案的方式。
System.IO 類別的設計可提供簡化的資料流撰寫。 您可以將基底資料流附加至一個或多個可提供您想要功能的傳遞資料流。 您可以將讀取器或寫入器附加至鏈結的結尾,以便可以輕易地讀取或寫入慣用的類型。
下列程式碼範例會在現有 MyFile.txt 的周圍建立 FileStream 以緩衝處理 MyFile.txt。 請注意,預設會緩衝處理 Filestream。
重要
範例假設名為 MyFile.txt 的檔案已與應用程式存在於相同的資料夾中。
範例:使用 StreamReader
下列範例會建立一個 StreamReader 來讀取 FileStream 中的字元,之後再傳遞至 StreamReader 作為其建構函式引數。 StreamReader.ReadLine 接著會一直讀取,直到 StreamReader.Peek 找不到其他字元。
using System;
using System.IO;
public class CompBuf
{
private const string FILE_NAME = "MyFile.txt";
public static void Main()
{
if (!File.Exists(FILE_NAME))
{
Console.WriteLine($"{FILE_NAME} does not exist!");
return;
}
// Create an instance of StreamReader characters from the file.
using (StreamReader sr = new StreamReader(FILE_NAME))
{
string input;
// While not at the end of the file, read lines from the file.
while (sr.Peek() > -1)
{
input = sr.ReadLine();
Console.WriteLine(input);
}
}
}
}
Imports System.IO
Public Class CompBuf
Private Const FILE_NAME As String = "MyFile.txt"
Public Shared Sub Main()
If Not File.Exists(FILE_NAME) Then
Console.WriteLine($"{FILE_NAME} does not exist!")
Return
End If
Dim fsIn As new FileStream(FILE_NAME, FileMode.Open, _
FileAccess.Read, FileShare.Read)
' Create an instance of StreamReader that can read
' characters from the FileStream.
Using sr As New StreamReader(fsIn)
Dim input As String
' While not at the end of the file, read lines from the file.
While sr.Peek() > -1
input = sr.ReadLine()
Console.WriteLine(input)
End While
End Using
End Sub
End Class
範例:使用 BinaryReader
下列範例會建立一個 BinaryReader 來讀取 FileStream 中的位元組,之後再傳遞至 BinaryReader 作為其建構函式引數。 ReadByte 接著會一直讀取,直到 PeekChar 找不到其他位元組。
using System;
using System.IO;
public class ReadBuf
{
private const string FILE_NAME = "MyFile.txt";
public static void Main()
{
if (!File.Exists(FILE_NAME))
{
Console.WriteLine($"{FILE_NAME} does not exist.");
return;
}
using (FileStream f = new FileStream(FILE_NAME, FileMode.Open, FileAccess.Read, FileShare.Read))
{
// Create an instance of BinaryReader that can
// read bytes from the FileStream.
using (BinaryReader br = new BinaryReader(f))
{
byte input;
// While not at the end of the file, read lines from the file.
while (br.PeekChar() > -1)
{
input = br.ReadByte();
Console.WriteLine(input);
}
}
}
}
}
Imports System.IO
Public Class ReadBuf
Private Const FILE_NAME As String = "MyFile.txt"
Public Shared Sub Main()
If Not File.Exists(FILE_NAME) Then
Console.WriteLine($"{FILE_NAME} does not exist.")
Return
End If
Dim f As New FileStream(FILE_NAME, FileMode.Open, _
FileAccess.Read, FileShare.Read)
' Create an instance of BinaryReader that can
' read bytes from the FileStream.
Using br As new BinaryReader(f)
Dim input As Byte
' While not at the end of the file, read lines from the file.
While br.PeekChar() > -1
input = br.ReadByte()
Console.WriteLine(input)
End While
End Using
End Sub
End Class
另請參閱
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應