إنشاء تدفقات

التخزين المساعد هو إحدى وسائط التخزين، مثل القرص أو الذاكرة. كل مخزن مساعد مختلف يقوم بتنفيذ الدفق الخاص به مثل تنفيذ الفئة Stream. كل نوع الدفق يقرأ ويكتب وحدات البايت من و إلى المخزن المساعد المُعطى. التدفقات التي تتصل بالمخازن المساعدة تسمى التدفقات الأساسية. التدفقات الأساسية لديها مُنشئات تحتوي على المعلمات الضرورية لتوصيل الدفق إلى المخزن المساعد. على سبيل المثال، FileStream لديه مُنشئات لتحديد معلمة المسار التي تحدد كيفية مشاركة الملف بواسطة العمليات ، إلخ.

يوفر تصميم الفئات System.IO إنشاء دفق مبسط. يمكن إرفاق التدفقات الأساسية إلي دفق تمريري واحد أو أكثر والذي يوفر لك الوظيفة التي تريدها. يمكن إرفاق قارئ أو كاتب في نهاية السلسلة بحيث يمكن قراءة الأنواع المفضلة أو كتابتها بسهولة.

يقوم مثال التعليمة البرمجية التالية بإنشاء FileStream حول MyFile.txt الموجودة مسبقاً لكي تقوم بالتخزين المؤقت MyFile.txt. (لاحظ أن FileStreams يتم تم تخزينها مؤقتاً بشكل افتراضي.) التالي، 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();
    }
}

يقوم مثال التعليمة البرمجية التالية بإنشاء FileStream حول MyFile.txt الموجودة مسبقاً لكي تقوم بالتخزين المؤقت MyFile.txt. (لاحظ أن FileStreams يتم تم تخزينها مؤقتاً بشكل افتراضي.) التالي، على 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();
    }
}

راجع أيضًا:

المرجع

StreamReader

StreamReader.ReadLine

StreamReader.Peek

FileStream

BinaryReader

BinaryReader.ReadByte

BinaryReader.PeekChar

المبادئ

ملفات I/O الأساسية

إنشاء كاتب