撰寫資料流
更新:2007 年 11 月
支援存放區為儲存媒體,例如磁碟或記憶體。各個不同的支援存放區都實作它自己的資料流做為 Stream 類別的實作。各個資料流類型都對它的指定支援存放區讀取並寫入位元組。連接至支援存放區的資料流稱為基底資料流。基底資料流擁有建構函式,其具備連接資料流至支援存放區的必要參數。例如,FileStream 具有指定路徑參數的建構函式,這參數可指定檔案將如何為處理序所共用,或諸如此類。
System.IO 類別的設計提供簡化的資料流撰寫方式。基底資料流可附加至一個或多個傳遞資料流,以提供想要的功能。讀取器或寫入器可附加至鏈結的結尾,以便能夠輕易讀取或寫入想要的型別。
下列程式碼範例針對現有的 MyFile.txt 建立 FileStream,以便緩衝 MyFile.txt(注意,預設的情況下,會緩衝 FileStream)。接下來,會建立 StreamReader 以從 FileStream (它會傳遞至 StreamReader 做為其建構函式引數) 讀取字元。ReadLine 會讀取直到 Peek 再也找不到字元為止。
Option Explicit On
Option Strict On
Imports System
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("{0} does not exist!", FILE_NAME)
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.
Dim sr As New StreamReader(fsIn)
' While not at the end of the file, read lines from the file.
While sr.Peek() > -1
Dim input As String = sr.ReadLine()
Console.WriteLine(input)
End While
sr.Close()
End Sub
End Class
using System;
using System.IO;
public class CompBuf
{
private const string FILE_NAME = "MyFile.txt";
public static void Main(String[] args)
{
if (!File.Exists(FILE_NAME))
{
Console.WriteLine("{0} does not exist!", FILE_NAME);
return;
}
FileStream fsIn = new FileStream(FILE_NAME, FileMode.Open,
FileAccess.Read, FileShare.Read);
// Create an instance of StreamReader that can read
// characters from the FileStream.
StreamReader sr = new StreamReader(fsIn);
// While not at the end of the file, read lines from the file.
while (sr.Peek()>-1)
{
String input = sr.ReadLine();
Console.WriteLine (input);
}
sr.Close();
}
}
下列程式碼範例針對現有的 MyFile.txt 建立 FileStream,以便緩衝 MyFile.txt(注意,預設的情況下,會緩衝 FileStream)。接下來,會建立 BinaryReader 以從 FileStream (它會傳遞至 BinaryReader 做為其建構函式引數) 讀取位元組。ReadByte 會讀取直到 PeekChar 再也找不到位元組為止。
Option Explicit On
Option Strict On
Imports System
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("{0} does not exist.", FILE_NAME)
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.
Dim sr As New BinaryReader(f)
' While not at the end of the file, read lines from the file.
While sr.PeekChar() > -1
Dim input As Byte = sr.ReadByte()
Console.WriteLine(input)
End While
sr.Close()
End Sub
End Class
using System;
using System.IO;
public class ReadBuf
{
private const string FILE_NAME = "MyFile.txt";
public static void Main(String[] args)
{
if (!File.Exists(FILE_NAME))
{
Console.WriteLine("{0} does not exist.", FILE_NAME);
return;
}
FileStream f = new FileStream(FILE_NAME, FileMode.Open,
FileAccess.Read, FileShare.Read);
// Create an instance of BinaryReader that can
// read bytes from the FileStream.
BinaryReader sr = new BinaryReader(f);
// While not at the end of the file, read lines from the file.
while (sr.PeekChar()>-1)
{
byte input = sr.ReadByte();
Console.WriteLine (input);
}
sr.Close();
}
}