StreamReader 類別

定義

實作以特定的編碼方式自位元組資料流讀取字元的 TextReader

public ref class StreamReader : System::IO::TextReader
public class StreamReader : System.IO.TextReader
[System.Serializable]
public class StreamReader : System.IO.TextReader
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public class StreamReader : System.IO.TextReader
type StreamReader = class
    inherit TextReader
[<System.Serializable>]
type StreamReader = class
    inherit TextReader
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type StreamReader = class
    inherit TextReader
Public Class StreamReader
Inherits TextReader
繼承
StreamReader
繼承
屬性

範例

下列範例會使用 的 StreamReader 實例來讀取檔案中的文字。 此範例中使用的建構函式不支援在 Windows 市集應用程式中使用。

using namespace System;
using namespace System::IO;
int main()
{
   try
   {
      // Create an instance of StreamReader to read from a file.
      StreamReader^ sr = gcnew StreamReader( "TestFile.txt" );
      try
      {
         String^ line;
         
         // Read and display lines from the file until the end of 
         // the file is reached.
         while ( line = sr->ReadLine() )
         {
            Console::WriteLine( line );
         }
      }
      finally
      {
         if ( sr )
            delete (IDisposable^)sr;
      }
   }
   catch ( Exception^ e ) 
   {
      // Let the user know what went wrong.
      Console::WriteLine( "The file could not be read:" );
      Console::WriteLine( e->Message );
   }
}
using System;
using System.IO;

class Test
{
    public static void Main()
    {
        try
        {
            // Create an instance of StreamReader to read from a file.
            // The using statement also closes the StreamReader.
            using (StreamReader sr = new StreamReader("TestFile.txt"))
            {
                string line;
                // Read and display lines from the file until the end of
                // the file is reached.
                while ((line = sr.ReadLine()) != null)
                {
                    Console.WriteLine(line);
                }
            }
        }
        catch (Exception e)
        {
            // Let the user know what went wrong.
            Console.WriteLine("The file could not be read:");
            Console.WriteLine(e.Message);
        }
    }
}
Imports System.IO

Class Test
    Public Shared Sub Main()
        Try
            ' Create an instance of StreamReader to read from a file.
            Dim sr As StreamReader = New StreamReader("TestFile.txt")
            Dim line As String
            ' Read and display the lines from the file until the end 
            ' of the file is reached.
            Do
                line = sr.ReadLine()
                Console.WriteLine(Line)
            Loop Until line Is Nothing
            sr.Close()
        Catch E As Exception
            ' Let the user know what went wrong.
            Console.WriteLine("The file could not be read:")
            Console.WriteLine(E.Message)
        End Try
    End Sub
End Class

下列範例會具現化 StreamReader 物件,並呼叫其 ReadAsync 方法以非同步方式讀取檔案。

using System;
using System.IO;
using System.Threading.Tasks;

class Example
{
    static async Task Main()
    {
        await ReadAndDisplayFilesAsync();
    }

    static async Task ReadAndDisplayFilesAsync()
    {
        String filename = "TestFile1.txt";
        Char[] buffer;

        using (var sr = new StreamReader(filename)) {
            buffer = new Char[(int)sr.BaseStream.Length];
            await sr.ReadAsync(buffer, 0, (int)sr.BaseStream.Length);
        }

        Console.WriteLine(new String(buffer));
    }
}
// The example displays the following output:
//       This is the first line of text in a relatively short file.
//       This is the second line.
//       This is the third line.
//       This is the fourth and final line.
Imports System.IO
Imports System.Threading.Tasks

Module Example
    Public Sub Main()
        ReadAndDisplayFilesAsync()
    End Sub

    Private Async Sub ReadAndDisplayFilesAsync()
        Dim filename As String = "TestFile1.txt"
        Dim buffer() As Char
        
        Using sr As New StreamReader(filename)
            ReDim buffer(CInt(sr.BaseStream.Length))
            Await sr.ReadAsync(buffer, 0, CInt(sr.BaseStream.Length))
        End Using

        Console.WriteLine(New String(buffer))
    End Sub
End Module
' The example displays the following output:
'       This is the first line of text in a relatively short file.
'       This is the second line.
'       This is the third line.
'       This is the fourth and final line.

備註

StreamReader 是針對特定編碼的字元輸入所設計,而 類別 Stream 則是針對位元組輸入和輸出所設計。 用於 StreamReader 從標準文字檔讀取資訊行。

重要

此型別代表 IDisposable 介面。 當您完成使用型別時,您應該直接或間接處置它。 若要直接處置型別,請呼叫其 try/catch 區塊中的 Dispose 方法。 若要間接處置它,請使用語言建構函式,例如 using (在 C# 中) 或 Using (在 Visual Basic 中)。 如需詳細資訊,請參閱 IDisposable 介面文章中的<使用實作 IDisposable 的物件>一節。

StreamReader 除非另有指定,否則預設為 UTF-8 編碼,而不是預設為目前系統的 ANSI 字碼頁。 UTF-8 會正確處理 Unicode 字元,並在當地語系化版本的作業系統上提供一致的結果。 如果您使用 屬性取得目前的字元編碼 CurrentEncoding ,則值在第一 Read 個方法之後才會可靠,因為直到第一次 Read 呼叫方法之前,才會完成編碼自動偵測。

根據預設,不是 StreamReader 安全線程。 如需安全線程包裝函式,請參閱 TextReader.Synchronized

Read(Char[], Int32, Int32)Write(Char[], Int32, Int32) 方法會多載參數所 count 指定的字元數,並加以寫入。 這些會與 BufferedStream.ReadBufferedStream.Write 區別,其可讀取和寫入 參數所 count 指定的位元組數目。 僅使用 BufferedStream 方法來讀取和寫入位元組陣列元素的整數編號。

注意

Stream 讀取 時,使用大小與資料流程內部緩衝區相同的緩衝區會更有效率。

如需一般 I/O 工作的清單,請參閱 一般 I/O 工作

建構函式

StreamReader(Stream)

為指定的資料流,初始化 StreamReader 類別的新執行個體。

StreamReader(Stream, Boolean)

使用指定的位元組順序標記偵測選項,針對指定的資料流初始化 StreamReader 類別的新執行個體。

StreamReader(Stream, Encoding)

使用指定的字元編碼,針對指定的資料流初始化 StreamReader 類別的新執行個體。

StreamReader(Stream, Encoding, Boolean)

使用指定的字元編碼和位元組順序標記偵測選項,為指定的資料流初始化 StreamReader 類別的新執行個體。

StreamReader(Stream, Encoding, Boolean, Int32)

使用指定的字元編碼、位元組順序標記偵測選項和緩衝區大小,為指定的資料流初始化 StreamReader 類別的新執行個體。

StreamReader(Stream, Encoding, Boolean, Int32, Boolean)

根據指定的字元編碼、位元順序標記偵測選項和緩衝區大小,為指定的資料流初始化 StreamReader 類別的新執行個體,並選擇性讓資料流保持開啟。

StreamReader(String)

為指定的檔名初始化 StreamReader 類別的新執行個體。

StreamReader(String, Boolean)

使用指定的位元組順序標記偵測選項,為指定的檔案名稱初始化 StreamReader 類別的新執行個體。

StreamReader(String, Encoding)

使用指定的字元編碼,針對指定的檔案名稱初始化 StreamReader 類別的新執行個體。

StreamReader(String, Encoding, Boolean)

使用指定的字元編碼和位元組順序標記偵測選項,為指定的檔案名稱初始化 StreamReader 類別的新執行個體。

StreamReader(String, Encoding, Boolean, FileStreamOptions)

使用指定的字元編碼、位元組順序標記偵測選項,並使用指定的 FileStreamOptions 物件來初始化 類別的新實例 StreamReader

StreamReader(String, Encoding, Boolean, Int32)

使用指定的字元編碼、位元組順序標記偵測選項和緩衝區大小,為指定的檔案名稱初始化 StreamReader 類別的新執行個體。

StreamReader(String, FileStreamOptions)

使用預設編碼方式,針對指定的檔案路徑,初始化 類別的新實例 StreamReader ,以在檔案開頭啟用位元組順序標記的偵測,並使用指定的 FileStreamOptions 物件進行設定。

欄位

Null

空資料流周圍的 StreamReader 物件。

屬性

BaseStream

傳回基礎資料流。

CurrentEncoding

取得目前 StreamReader 物件使用的目前字元編碼。

EndOfStream

取得表示現行資料流位置是否在資料流結尾的值。

方法

Close()

關閉 StreamReader 物件和基礎資料流,並釋放任何與讀取器相關聯的系統資源。

Close()

關閉 TextReader 並釋放任何與 TextReader 關聯的系統資源。

(繼承來源 TextReader)
CreateObjRef(Type)

建立包含所有相關資訊的物件,這些資訊是產生用來與遠端物件通訊的所需 Proxy。

(繼承來源 MarshalByRefObject)
DiscardBufferedData()

清除內部緩衝區。

Dispose()

釋放由 TextReader 物件使用的所有資源。

(繼承來源 TextReader)
Dispose(Boolean)

關閉基礎資料流,接著釋放 StreamReader 所使用的 Unmanaged 資源,然後再選擇性釋放 Managed 資源。

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetLifetimeService()
已淘汰.

擷取控制這個執行個體存留期 (Lifetime) 原則的目前存留期服務物件。

(繼承來源 MarshalByRefObject)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
InitializeLifetimeService()
已淘汰.

取得存留期服務物件,以控制這個執行個體的存留期原則。

(繼承來源 MarshalByRefObject)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
MemberwiseClone(Boolean)

建立目前 MarshalByRefObject 物件的淺層複本。

(繼承來源 MarshalByRefObject)
Peek()

傳回下一個可供使用的字元,但不使用它。

Read()

自輸入資料流讀取下一個字元,並將字元位置前移一個字元。

Read(Char[], Int32, Int32)

從目前資料流讀取指定的字元數目上限,在指定的索引位置開始讀入緩衝區中。

Read(Span<Char>)

將目前資料流中的字元讀入至範圍。

Read(Span<Char>)

從目前的讀取器讀取字元,並將資料寫入至指定的緩衝區。

(繼承來源 TextReader)
ReadAsync(Char[], Int32, Int32)

從目前的資料流非同步讀取指定的取大字元數目,並從指定的索引開始將資料寫入緩衝區。

ReadAsync(Char[], Int32, Int32)

從目前的文字讀取器非同步讀取指定的最大字元數目,並從指定的索引開始將資料寫入緩衝區。

(繼承來源 TextReader)
ReadAsync(Memory<Char>, CancellationToken)

以非同步方式將目前資料流中的字元讀入至記憶體區塊。

ReadAsync(Memory<Char>, CancellationToken)

以非同步方式將目前資料流中的字元讀入至記憶體區塊。

(繼承來源 TextReader)
ReadBlock(Char[], Int32, Int32)

從目前資料流讀取指定的最大字元數目,並從指定的索引開始將資料寫入緩衝區。

ReadBlock(Char[], Int32, Int32)

從目前的文字讀取器讀取指定的最大字元數目,並從指定的索引開始將資料寫入緩衝區。

(繼承來源 TextReader)
ReadBlock(Span<Char>)

從目前的資料流讀取字元,並將資料寫入至緩衝區。

ReadBlock(Span<Char>)

從目前的資料流讀取字元,並將資料寫入至緩衝區。

(繼承來源 TextReader)
ReadBlockAsync(Char[], Int32, Int32)

從目前的資料流非同步讀取指定的取大字元數目,並從指定的索引開始將資料寫入緩衝區。

ReadBlockAsync(Char[], Int32, Int32)

從目前的文字讀取器非同步讀取指定的最大字元數目,並從指定的索引開始將資料寫入緩衝區。

(繼承來源 TextReader)
ReadBlockAsync(Memory<Char>, CancellationToken)

以非同步方式從目前的資料流讀取字元,並將資料寫入至緩衝區。

ReadBlockAsync(Memory<Char>, CancellationToken)

以非同步方式從目前的資料流讀取字元,並將資料寫入至緩衝區。

(繼承來源 TextReader)
ReadLine()

自目前資料流讀取一行字元,並將資料以字串傳回。

ReadLineAsync()

自目前資料流非同步讀取一行字元,並將資料以字串傳回。

ReadLineAsync()

非同步讀取一行字元,並將資料以字串傳回。

(繼承來源 TextReader)
ReadLineAsync(CancellationToken)

自目前資料流非同步讀取一行字元,並將資料以字串傳回。

ReadLineAsync(CancellationToken)

非同步讀取一行字元,並將資料以字串傳回。

(繼承來源 TextReader)
ReadToEnd()

讀取從目前位置到資料流末端的所有字元。

ReadToEndAsync()

非同步讀取從目前位置到資料流末端的所有字元,並將它們以字串傳回。

ReadToEndAsync()

非同步讀取從目前位置到文字讀取器末端的所有字元,並將它們以單一字串傳回。

(繼承來源 TextReader)
ReadToEndAsync(CancellationToken)

非同步讀取從目前位置到資料流末端的所有字元,並將它們以字串傳回。

ReadToEndAsync(CancellationToken)

非同步讀取從目前位置到文字讀取器末端的所有字元,並將它們以單一字串傳回。

(繼承來源 TextReader)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

明確介面實作

IDisposable.Dispose()

如需這個成員的說明,請參閱 Dispose()

(繼承來源 TextReader)

適用於

另請參閱