撰写流

后备存储是磁盘或内存等存储介质。 各个后备存储都实现自己的流,作为 Stream 类的实现。

每个流类型对给定的后备存储执行字节读取和写入操作。 连接到后备存储的流称为“基流”。 基流的构造函数包含将流连接到后备存储所需的参数。 例如,FileStream 包含指定路径参数的构造函数,此参数指定了进程如何共享文件。

System.IO 类旨在简化流撰写。 可以将基流附加到一个或多个提供所需功能的直通流。 可以将读取器或编写器附加到链的末尾,这样便能轻松读取或编写首选类型。

下面的代码示例根据现有 MyFile.txt 创建了 FileStream,以便缓冲 MyFile.txt。 请注意,FileStreams 默认缓冲

重要

这些示例假定名为 MyFile.txt 的文件已存在于与应用相同的文件夹中

示例:使用 StreamReader

以下示例将创建从 FileStream 读取字符的 StreamReader,此读取器作为构造函数参数传递给 StreamReaderStreamReader。 除非 StreamReader.Peek 找不到更多字符,否则 StreamReader.ReadLine 会一直读取字符。

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

以下示例将创建从 FileStream 读取字节的 BinaryReader,此读取器作为构造函数参数传递给 BinaryReaderBinaryReader。 除非 PeekChar 找不到更多字节,否则 ReadByte 会一直读取字节。

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

另请参阅