[VB.NET][C#.NET] MemoryStream / BufferedStream 類別

[VB.NET][C#.NET] MemoryStream / BufferedStream 類別

MemoryStreamStringReader / StringWriter 類別用法差不多,但兩者還是些微不同;MemoryStream可以將已經寫入記憶體的資料,直接另存到實體檔案,而StringWriter不能直接存。

        private void button1_Click(object sender, EventArgs e)
        {
            //引用MemoryStream類別
            MemoryStream myMem = new MemoryStream();
            //引用StreamWriter類別
            StreamWriter myWriter = new StreamWriter(myMem);
            //寫資料至記憶體
            myWriter.Write("寫資料至:");
            myWriter.WriteLine("記憶體(Memory)");
            //寫資料至檔案
            myWriter.Flush();
            FileStream myFile = File.Open(@"C:\myFile.txt", FileMode.OpenOrCreate);
            myMem.WriteTo(myFile);
            //釋放
            myFile.Dispose();
            myWriter.Dispose();
            myMem.Dispose();
 
        }

 

 

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        '引用MemoryStream類別
        Dim myMem As MemoryStream = New MemoryStream()
        '引用StreamWriter類別
        Dim myWriter As StreamWriter = New StreamWriter(myMem)
        '寫資料至記憶體
        myWriter.Write("寫資料至:")
        myWriter.WriteLine("記憶體(Memory)")
        '寫資料至檔案
        myWriter.Flush()
        Dim myFile As FileStream = File.Open("C:\myFile.txt", FileMode.OpenOrCreate)
        myMem.WriteTo(myFile)
        '釋放
        myFile.Dispose()
        myWriter.Dispose()
        myMem.Dispose()
    End Sub

 

BufferedStream 類別,是將緩衝層加入另一資料流上的讀寫。

有時候直接將資料寫入檔案效能並不好,由其是在處理大筆資料時,為了提高處理效率可以使用BufferedStream 類別,將資料寫入緩衝區內,當緩衝區被全部清除輸出時,資料才會真正的被寫入資料。

        private void button2_Click(object sender, EventArgs e)
        {
            FileStream myFile = File.Open(@"C:\myFile.txt", FileMode.Create, FileAccess.Write, FileShare.Write);
            BufferedStream myBuf = new BufferedStream(myFile);
            StreamWriter myWrite = new StreamWriter(myBuf);
 
            myWrite.Write("寫入緩衝區");
            myWrite.WriteLine("步驟還真麻煩");
 
            myBuf.Flush();
            //釋放
            myWrite.Dispose();
            myBuf.Dispose();
            myFile.Dispose();
        }

 

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim myFile As FileStream = File.Open("C:\myFile.txt", FileMode.Create, FileAccess.Write, FileShare.Write)
        Dim myBuf As BufferedStream = New BufferedStream(myFile)
        Dim myWrite As StreamWriter = New StreamWriter(myBuf)
 
        myWrite.Write("寫入緩衝區")
        myWrite.WriteLine("步驟還真麻煩")
 
        myBuf.Flush()
        '釋放
        myWrite.Dispose()
        myBuf.Dispose()
        myFile.Dispose()
    End Sub

若有謬誤,煩請告知,新手發帖請多包涵


Microsoft MVP Award 2010~2017 C# 第四季
Microsoft MVP Award 2018~2022 .NET

Image result for microsoft+mvp+logo