ストリームの構成

更新 : 2007 年 11 月

バッキング ストアとは、ディスクやメモリのようなストレージ メディアのことです。各種のバッキング ストアのそれぞれが、Stream クラスの実装として独自のストリームを実装しています。各ストリーム型は、関連付けられたバッキング ストアからバイトを読み取ったり書き込んだりします。バッキング ストアに関連付けられたストリームは、基本ストリームと呼ばれます。基本ストリームには、ストリームをバッキング ストアに関連付けるために必要なパラメータを持つコンストラクタがあります。たとえば、FileStream には、プロセス間でファイルを共有する方法を指定するパス パラメータなどを指定するコンストラクタがあります。

System.IO クラスは、ストリームを簡単に構成できるようにデザインされています。基本ストリームは、必要な機能を提供する 1 つ以上のパススルー ストリームに結合できます。必要な型を簡単に読み取ったり書き込んだりできるように、チェインの末尾にリーダーまたはライタを結合できます。

既存の MyFile.txt をバッファリングするために、MyFile.txt に対して FileStream を作成するコードの例を次に示します。FileStreams は、既定でバッファリングされます。次に、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 をバッファリングするために、MyFile.txt に対して FileStream を作成するコードの例を次に示します。FileStreams は、既定でバッファリングされます。次に、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();
    }
}

参照

概念

基本のファイル I/O

ライタの作成

参照

StreamReader

StreamReader.ReadLine

StreamReader.Peek

FileStream

BinaryReader

BinaryReader.ReadByte

BinaryReader.PeekChar