C#高效的读取大型文本文件的方法

本文转载连接: https://blog.csdn.net/hust_zhang/article/details/6307055

ufferedStream+FileStream+StreamReader

开个10M缓冲池,然后加上异步,速度飞飞的。

MemoryStream 跟 StringReader / 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

你可能感兴趣的:(c#)