Composing Streams

A backing store is a storage medium, such as a disk or memory. Each different backing store implements its own stream as an implementation of the Stream class. Each stream type reads and writes bytes from and to its given backing store. Streams that connect to backing stores are called base streams. Base streams have constructors that have the parameters necessary to connect the stream to the backing store. For example, FileStream has constructors that specify a path parameter, which specifies how the file will be shared by processes, and so on.

The design of the System.IO classes provides simplified stream composing. Base streams can be attached to one or more pass-through streams that provide the functionality you want. A reader or writer can be attached to the end of the chain so that the preferred types can be read or written easily.

The following code example creates a FileStream around the existing MyFile.txt in order to buffer MyFile.txt. (Note that FileStreams are buffered by default.) Next, a StreamReader is created to read characters from the FileStream, which is passed to the StreamReader as its constructor argument. ReadLine reads until Peek finds no more characters.

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();
    }
}

The following code example creates a FileStream around the existing MyFile.txt in order to buffer MyFile.txt. (Note that FileStreams are buffered by default.) Next, a BinaryReader is created to read bytes from the FileStream, which is passed to the BinaryReader as its constructor argument. ReadByte reads until PeekChar finds no more bytes.

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();
    }
}

See Also

Concepts

Basic File I/O

Creating a Writer

Reference

StreamReader

StreamReader.ReadLine

StreamReader.Peek

FileStream

BinaryReader

BinaryReader.ReadByte

BinaryReader.PeekChar