C# 中的 TextReader 用於從文字檔案讀取文字或連續的字元序列。 TextReader 類別位於 System.IO 命名空間下。它是StreamReader和StringReader的抽象基底類,分別用於從流和字串中讀取字元。我們無法建立 TextReader 的對象,因為它是一個抽象類別。預設情況下,TextReader 不是線程安全的。派生 TextReader 類別的類別需要至少實作 Peek() 和 Read() 方法才能建立有用的 TextReader 實例。
文法:
建立TextReader的語法如下:
TextReader text_reader = File.OpenText(file_path);
上述語句將在「file_path」指定的位置開啟一個檔案。然後,在 text_reader 的幫助下,我們可以使用 TextReader 類別的方法來從檔案中讀取內容。
我們也可以藉助「using」區塊建立 TextReader,如下所示:
using(TextReader text_reader = File.OpenText(file_path)) { //user code }
使用「using」區塊的優點是,當物件的工作完成並且不再需要該物件時,它會釋放其中指定的物件所獲取的記憶體。
為了使用 TextReader,有必要在我們的程式碼中匯入 System.IO 命名空間。由於 TextReader 是一個抽象類,我們不能直接使用 new 關鍵字來建立它的實例,但我們可以使用 File 類別的 OpenText() 方法來實現,如下所示:
TextReader text_reader = File.OpenText(file_path);
OpenText() 方法將檔案的位置作為輸入,然後在同一位置開啟現有的 UTF-8 編碼文字檔案進行讀取。
File.OpenText() 方法傳回 StreamReader 類別的對象,該類別是 TextReader 的衍生類,因此有助於在程式碼中建立 TextReader 類別的有用實例。此實例可用於呼叫 TextReader 類別的方法來從檔案中讀取內容。 TextReader 類別衍生自抽象類別 MarshalByRefObject。其繼承層次如下圖所示:
物件 → MarshalByRefObject → TextReader
我們可以在 TextReader 的兩個衍生類別(即 StreamReader 和 StringReader)的協助下使用 TextReader。
TextReader的一些重要方法請參考下表:
Method | Description |
Close() | It is used to close the TextReader and to release any system resources associated with it. |
Dispose() | It is used to release all the resources used by an object of TextReader. |
Peek() | It is used to read the next character without changing the state of the reader and it returns the next available character without actually reading it from the reader. |
Read() | It is used to read the next character from the text reader and it also advances the character position by one character. |
ReadLine() | It is used to read a line of characters from the text reader and it also returns the data as a string. |
ReadToEnd() | It is used to read all characters from the current position to the end of the text reader and it returns them as one string. |
We can pass a text file name in a TextReader constructor to create an object. Following are the different examples of TextReader in C#.
Reading a line of a file using the ReadLine() method of TextReader.
Code:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; namespace ConsoleApp3 { public class Program { public static void Main() { string file = @"E:\Content\TextReader.txt"; try { if (File.Exists(file)) { // opening the text file and reading a line using (TextReader textReader = File.OpenText(file)) { Console.WriteLine(textReader.ReadLine()); } } else { Console.WriteLine("File does not exist!"); } Console.ReadKey(); } catch (Exception ex) { Console.WriteLine(ex.Message); } } } }
Output:
Reading five characters from a file using the ReadBlock() method of TextReader.
Code:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; namespace ConsoleApp3 { public class Program { public static void Main() { string file = @"E:\Content\TextReader.txt"; try { if (File.Exists(file)) { //Opening the text file and reading 5 characters using (TextReader textReader = File.OpenText(file)) { char[] ch = new char[5]; textReader.ReadBlock(ch, 0, 5); Console.WriteLine(ch); } } else { Console.WriteLine("File does not exist!"); } Console.ReadKey(); } catch (Exception ex) { Console.WriteLine(ex.Message); } } } }
Output:
Reading the whole content of a text file using the ReadToEnd() method of TextReader.
Code:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; namespace ConsoleApp3 { public class Program { public static void Main() { string file = @"E:\Content\TextReader.txt"; string content = String.Empty; try { if (File.Exists(file)) { //Opening a text file and reading the whole content using (TextReader tr = File.OpenText(file)) { content = tr.ReadToEnd(); Console.WriteLine(content); } } else { Console.WriteLine("File does not exist!"); } Console.ReadKey(); } catch (Exception ex) { Console.WriteLine(ex.Message); } } } }
Output:
Reading the content of a text file using TextReader and writing it to another file.
Code:
using System; using System.Text; using System.Threading.Tasks; using System.IO; namespace ConsoleApp3 { public class Program { public static void Main() { string fileToRead = @"E:\Content\TextReader.txt"; string fileToWrite = @"E:\Content\TextReaderAndWriter.txt"; StringBuilder content = new StringBuilder(); string str = String.Empty; try { //checking if the file exists to read if (File.Exists(fileToRead)) { //Opening a text file and reading the whole content using (TextReader textReader = File.OpenText(fileToRead)) { while ((str = textReader.ReadLine()) != null) { content.Append("\n" + str); } } } else { Console.WriteLine("File does not exist!"); } //checking if the file to write content already exists if (File.Exists(fileToWrite)) { File.Delete(fileToWrite); } //creating file if it does not exist using (TextWriter textWriter = File.CreateText(fileToWrite)) { textWriter.WriteLine(content); } Console.ReadKey(); } catch (Exception ex) { Console.WriteLine(ex.Message); } } } }
Output:
以上是C# 中的文字閱讀器的詳細內容。更多資訊請關注PHP中文網其他相關文章!