Composizione dei flussiComposing Streams

Un archivio di backup è un supporto di archiviazione, ad esempio un disco o la memoria.A backing store is a storage medium, such as a disk or memory. Ogni tipo di archivio di backup implementa il flusso come implementazione della classe Stream.Each different backing store implements its own stream as an implementation of the Stream class. Ogni tipo di flusso legge e scrive i byte da e nel relativo archivio di backup.Each stream type reads and writes bytes from and to its given backing store. I flussi che si connettono agli archivi di backup sono chiamati flussi di base.Streams that connect to backing stores are called base streams. I flusso di base hanno costruttori con i parametri necessari per connettere il flusso all'archivio di backup.Base streams have constructors that have the parameters necessary to connect the stream to the backing store. FileStream, ad esempio, ha costruttori che specificano un parametro del percorso, che specifica come il file verrà condiviso dai processi e così via.For example, FileStream has constructors that specify a path parameter, which specifies how the file will be shared by processes, and so on.

La progettazione delle classi System.IO consente una composizione dei flussi semplificata.The design of the System.IO classes provides simplified stream composing. I flussi di base possono essere collegati a uno o più flussi pass-through che forniscono la funzionalità desiderata.Base streams can be attached to one or more pass-through streams that provide the functionality you want. Un reader o un writer può essere accodato alla fine della catena in modo che i tipi preferiti possano essere letti o scritti facilmente.A reader or writer can be attached to the end of the chain so that the preferred types can be read or written easily.

L'esempio di codice seguente crea un elemento FileStream attorno all'elemento MyFile.txt esistente per memorizzare nel buffer MyFile.txt.The following code example creates a FileStream around the existing MyFile.txt in order to buffer MyFile.txt. Si noti che gli elementi FileStream vengono memorizzati nel buffer per impostazione predefinita. Viene quindi creato un elemento StreamReader per leggere i caratteri dal FileStream, che viene passato allo StreamReader come argomento del costruttore.(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 legge finché Peek non trova più caratteri.ReadLine reads until Peek finds no more characters.

using namespace System;
using namespace System::IO;

public ref class CompBuf
{
private:
    static String^ FILE_NAME = "MyFile.txt";

public:
    static void Main()
    {
        if (!File::Exists(FILE_NAME))
        {
            Console::WriteLine("{0} does not exist!", FILE_NAME);
            return;
        }
        FileStream^ fsIn = gcnew FileStream(FILE_NAME, FileMode::Open,
            FileAccess::Read, FileShare::Read);
        // Create an instance of StreamReader that can read
        // characters from the FileStream.
        StreamReader^ sr = gcnew StreamReader(fsIn);
        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);
        }
        sr->Close();
    }
};

int main()
{
    CompBuf::Main();
}
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("{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.
        using (StreamReader sr = new StreamReader(fsIn))
        {
            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
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.
        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

L'esempio di codice seguente crea un elemento FileStream attorno all'elemento MyFile.txt esistente per memorizzare nel buffer MyFile.txt.The following code example creates a FileStream around the existing MyFile.txt in order to buffer MyFile.txt. Si noti che gli elementi FileStream vengono memorizzati nel buffer per impostazione predefinita. Viene quindi creato un BinaryReader per leggere i byte dal FileStream, che viene passato al BinaryReader come argomento del costruttore.(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 legge finché PeekChar non trova più byte.ReadByte reads until PeekChar finds no more bytes.

using namespace System;
using namespace System::IO;

public ref class ReadBuf
{
private:
    static String^ FILE_NAME = "MyFile.txt";

public:
    static void Main()
    {
        if (!File::Exists(FILE_NAME))
        {
            Console::WriteLine("{0} does not exist.", FILE_NAME);
            return;
        }
        FileStream^ f = gcnew FileStream(FILE_NAME, FileMode::Open,
            FileAccess::Read, FileShare::Read);
        // Create an instance of BinaryReader that can
        // read bytes from the FileStream.
        BinaryReader^ br = gcnew 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);
        }
        br->Close();
    }
};

int main()
{
    ReadBuf::Main();
}
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("{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.
        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
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.
        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

Vedere ancheSee Also

StreamReader
StreamReader.ReadLine
StreamReader.Peek
FileStream
BinaryReader
BinaryReader.ReadByte
BinaryReader.PeekChar