构成流

更新:2007 年 11 月

备份存储区是一个存储媒介,例如磁盘或内存。每个不同的备份存储区都实现其自己的流作为 Stream 类的实现。每个流类型也都从其给定的备份存储区读取字节并向其给定的备份存储区写入字节。连接到备份存储区的流叫做基流。基流具有的构造函数具有将流连接到备份存储区所需的参数。例如,FileStream 具有指定路径参数(指定进程将如何共享文件的参数)等的构造函数。

System.IO 类的设计提供简化的流构成。可以将基流附加到一个或多个提供所需功能的传递流。读取器或编写器可以附加到链的末端,这样便可以方便地读取或写入所需的类型。

下面的代码示例围绕现有 MyFile.txt 创建 FileStream,为 MyFile.txt 提供缓冲。(请注意,默认情况下缓冲 FileStreams。) 然后,创建 StreamReader 以读取 FileStream 中的字符,FileStream 被作为 StreamReader 的构造函数参数传递给 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 提供缓冲。(请注意,默认情况下缓冲 FileStreams。) 然后,创建 BinaryReader 以读取 FileStream 中的字节,FileStream 被作为 BinaryReader 的构造函数参数传递给 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();
    }
}

请参见

概念

基本的文件 I/O

创建编写器

参考

StreamReader

StreamReader.ReadLine

StreamReader.Peek

FileStream

BinaryReader

BinaryReader.ReadByte

BinaryReader.PeekChar