C# System.IO.StreamReader


實現一個 TextReader,使其以一種特定的編碼從字節流中讀取字符。

 1 using System;
 2 using System.IO;
 3 
 4 class Test 
 5 {
 6     public static void Main() 
 7     {
 8         try 
 9         {
10             // Create an instance of StreamReader to read from a file.
11             // The using statement also closes the StreamReader.
12             using (StreamReader sr = new StreamReader("TestFile.txt")) 
13             {
14                 string line;
15                 // Read and display lines from the file until the end of 
16                 // the file is reached.
17                 while ((line = sr.ReadLine()) != null) 
18                 {
19                     Console.WriteLine(line);
20                 }
21             }
22         }
23         catch (Exception e) 
24         {
25             // Let the user know what went wrong.
26             Console.WriteLine("The file could not be read:");
27             Console.WriteLine(e.Message);
28         }
29     }
30 }

 

構造函數 

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, Int32)

為指定的文件名初始化 StreamReader 類的新實例,帶有指定字符編碼、字節順序標記檢測選項和緩沖區大小。

字段 

Null

空流周圍的 StreamReader

屬性 

BaseStream

返回基礎流。

CurrentEncoding

獲取當前 StreamReader 對象正在使用的當前字符編碼。

EndOfStream

獲取一個值,該值表示當前的流位置是否在流的末尾。

方法 

Close()

關閉 StreamReader 對象和基礎流,並釋放與讀取器關聯的所有系統資源。

CreateObjRef(Type)

創建一個對象,該對象包含生成用於與遠程對象進行通信的代理所需的全部相關信息。

(Inherited from MarshalByRefObject)
DiscardBufferedData()

清除內部緩沖區。

Dispose()

釋放由 TextReader 對象使用的所有資源。

(Inherited from TextReader)
Dispose(Boolean)

關閉基礎流,釋放 StreamReader 使用的未托管資源,同時還可以根據需要釋放托管資源。

Equals(Object)

確定指定的對象是否等於當前對象。

(Inherited from Object)
GetHashCode()

作為默認哈希函數。

(Inherited from Object)
GetLifetimeService()

檢索控制此實例的生存期策略的當前生存期服務對象。

(Inherited from MarshalByRefObject)
GetType()

獲取當前實例的 Type

(Inherited from Object)
InitializeLifetimeService()

獲取生存期服務對象來控制此實例的生存期策略。

(Inherited from MarshalByRefObject)
MemberwiseClone()

創建當前 Object 的淺表副本。

(Inherited from Object)
MemberwiseClone(Boolean)

創建當前 MarshalByRefObject 對象的淺表副本。

(Inherited from MarshalByRefObject)
Peek()

返回下一個可用的字符,但不使用它。

Read()

讀取輸入流中的下一個字符並使該字符的位置提升一個字符。

Read(Char[], Int32, Int32)

從當前流中將指定的最多個字符讀到指定索引位置開始的緩沖區。

ReadAsync(Char[], Int32, Int32)

從當前流中異步讀取指定的最大字符,並且從指定的索引位置開始將該數據寫入緩沖區。

ReadBlock(Char[], Int32, Int32)

從當前流中讀取指定數目的字符並從指定索引開始將該數據寫入緩沖區。

ReadBlockAsync(Char[], Int32, Int32)

從當前流中異步讀取指定的最大字符,並且從指定的索引位置開始將該數據寫入緩沖區。

ReadLine()

從當前流中讀取一行字符並將數據作為字符串返回。

ReadLineAsync()

從當前流中異步讀取一行字符並將數據作為字符串返回。

ReadToEnd()

從流的當前位置到末尾讀取所有字符。

ReadToEndAsync()

異步讀取從當前位置到流的結尾的所有字符並將它們作為一個字符串返回。

ToString()

返回表示當前對象的字符串。

(Inherited from Object)
IDisposable.Dispose()

有關此成員的說明,請參見 Dispose()

(Inherited from TextReader)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM