StreamReader.DiscardBufferedData StreamReader.DiscardBufferedData StreamReader.DiscardBufferedData StreamReader.DiscardBufferedData Method


清除內部緩衝區。Clears the internal buffer.

 void DiscardBufferedData();
public void DiscardBufferedData ();
member this.DiscardBufferedData : unit -> unit
Public Sub DiscardBufferedData ()


下列範例顯示的DiscardBufferedData案例中,必須呼叫方法來同步處理內部緩衝區和基礎資料流程。The following example shows a scenario where the DiscardBufferedData method must be called to synchronize the internal buffer and the underlying stream. 範例中的檔案是用來說明位置,並包含文字abcdefghijklmnopqrstuvwxyzThe file in the example is used to illustrate position and consists of the text abcdefghijklmnopqrstuvwxyz. 藉由DiscardBufferedData在讀取資料後呼叫,此範例會如預期般運作。By calling DiscardBufferedData after the data is read, the example works as expected. 讀取前15個字元之後,會將位置重設為位移值2,並讀取所有剩餘的字元。After the first 15 characters are read, the position is reset to the offset value of 2 and all the remaining characters are read. 如果您移除對的呼叫DiscardBufferedData,此範例不會如預期般運作。If you remove the call to DiscardBufferedData, the example does not work as expected. 系統會讀取前15個字元,但只會重設基礎資料流程的位置。The first 15 characters are read, but only the position of the underlying stream is reset. StreamReader物件的內部緩衝區仍在16個字元。The internal buffer of the StreamReader object is still on the 16th character. 因此, ReadToEnd會從重設位置開始,傳回緩衝區中的所有字元加上基礎資料流程中的字元。Therefore, ReadToEnd returns all the characters in the buffer plus the characters in the underlying stream starting from the reset position.

using System;
using System.IO;

class Test
    public static void Main()
        string path = @"c:\temp\alphabet.txt";

        using (StreamReader sr = new StreamReader(path))
            char[] c = null;

            c = new char[15];
            sr.Read(c, 0, c.Length);
            Console.WriteLine("first 15 characters:");
            // writes - "abcdefghijklmno"
            sr.BaseStream.Seek(2, SeekOrigin.Begin);
            Console.WriteLine("\nBack to offset 2 and read to end: ");
            // writes - "cdefghijklmnopqrstuvwxyz"
            // without DiscardBufferedData, writes - "pqrstuvwxyzcdefghijklmnopqrstuvwxyz"
Imports System.IO

Module Module1

    Sub Main()
        Dim path As String = "c:\temp\alphabet.txt"

        Dim sr As StreamReader = New StreamReader(path)
        Dim c(14) As Char

        sr.Read(c, 0, c.Length)
        Console.WriteLine("first 15 characters:")
        ' writes - "abcdefghijklmno"

        sr.BaseStream.Seek(2, SeekOrigin.Begin)
        Console.WriteLine(vbNewLine & "Back to offset 2 and read to end: ")
        ' writes - "cdefghijklmnopqrstuvwxyz"
        ' without DiscardBufferedData, writes - "pqrstuvwxyzcdefghijklmnopqrstuvwxyz"
    End Sub

End Module


使用方法,即可重設StreamReader物件的內部緩衝區。 DiscardBufferedDataUse the DiscardBufferedData method to reset the internal buffer for the StreamReader object. 只有當內部緩衝區的位置和BaseStream不相符時,才需要呼叫這個方法。You need to call this method only when the position of the internal buffer and the BaseStream do not match. 當您將資料讀入緩衝區,然後在基礎資料流程中搜尋新的位置時,這些位置可能會變得不相符。These positions can become mismatched when you read data into the buffer and then seek a new position in the underlying stream. 這個方法會降低效能,而且只有在絕對必要時才使用,例如當您想要多次讀取StreamReader物件內容的一部分時。This method slows performance and should be used only when absolutely necessary, such as when you want to read a portion of the contents of a StreamReader object more than once.

如需一般 i/o 工作的清單, 請參閱一般 i/o工作。For a list of common I/O tasks, see Common I/O Tasks.