<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # 在 C# 中讀取文本文件 > 原文: [https://zetcode.com/csharp/readtext/](https://zetcode.com/csharp/readtext/) 在本文中,我們展示了如何在 C# 中讀取文本文件。 [C# 教程](http://zetcode.com/lang/csharp/)是有關 C# 語言的綜合教程。 C# 中的輸入&輸出基于流。 `Stream`是所有流的抽象基類。 流是字節序列的抽象,例如文件,輸入/輸出設備,進程間通信管道或 TCP/IP 套接字。 ## C# 流 `Stream`為輸入和輸出的類型提供通用接口,并將編程器與操作系統和底層設備的特定詳細信息隔離開。 例如,`MemoryStream`處理內存中的數據,`FileStream`處理文件中的數據。 `thermopylae.txt` ```cs The Battle of Thermopylae was fought between an alliance of Greek city-states, led by King Leonidas of Sparta, and the Persian Empire of Xerxes I over the course of three days, during the second Persian invasion of Greece. ``` 在我們的示例中,我們將讀取以下文件: ## C# 使用`File.ReadAllText`讀取文本文件 `File.ReadAllText()`方法打開一個文本文件,將文件的所有行讀取為字符串,然后關閉文件。 `Program.cs` ```cs using System; using System.IO; using System.Text; namespace ReadAllText { class Program { static void Main(string[] args) { var path = @"C:\Users\Jano\Documents\thermopylae.txt"; string content = File.ReadAllText(path, Encoding.UTF8); Console.WriteLine(content); } } } ``` 該示例讀取`thermopylae.txt`文件的內容并將其打印到控制臺。 ## C# 使用`File.ReadAllLines`讀取文本文件 `File.ReadAllLines()`打開一個文本文件,將文件的所有行讀入字符串數組,然后關閉文件。 `Program.cs` ```cs using System; using System.IO; using System.Text; namespace ReadAllLines { class Program { static void Main(string[] args) { var path = @"C:\Users\Jano\Documents\thermopylae.txt"; string[] lines = File.ReadAllLines(path, Encoding.UTF8); foreach (string line in lines) { Console.WriteLine(line); } } } } ``` 使用`File.ReadAllLines()`方法讀取`thermopylae.txt`文件的內容并將其打印到控制臺。 ```cs foreach (string line in lines) { Console.WriteLine(line); } ``` 我們遍歷數組并打印其元素。 ## C# 使用`StreamReader`讀取文本文件 `StreamReader`設計用于以特定編碼輸入字符。 它用于從標準文本文件中讀取信息行。 ### 使用`StreamReader`的`ReadToEnd` `ReadToEnd()`方法從流的當前位置到其末尾讀取所有字符。 `Program.cs` ```cs using System; using System.IO; using System.Text; namespace StreamReaderReadToEnd { class Program { static void Main(string[] args) { var path = @"C:\Users\Jano\Documents\thermopylae.txt"; using var fs = new FileStream(path, FileMode.Open, FileAccess.Read); using var sr = new StreamReader(fs, Encoding.UTF8); string content = sr.ReadToEnd(); Console.WriteLine(content); } } } ``` 該示例使用`StreamReader`的`ReadToEnd()`方法讀取文件。 ```cs using var fs = new FileStream(path, FileMode.Open, FileAccess.Read); ``` `FileStream`類為文件提供`Stream`,支持同步和異步讀取和寫入操作。 構造器使用指定的路徑,創建模式和讀/寫權限初始化`FileStream`類的新實例。 ```cs using var sr = new StreamReader(fs, Encoding.UTF8); ``` `FileStream`被傳遞到`StreamReader`。 ```cs string content = sr.ReadToEnd(); ``` `StreamReader`的`ReadToEnd()`方法讀取從當前位置到文件結尾的所有字符。 ### 使用`StreamReader`的`ReadLine` `StreamReader`的`ReadLine()`方法從當前流中讀取一行字符,并將數據作為字符串返回。 `Program.cs` ```cs using System; using System.IO; using System.Text; namespace StreamReaderReadLine { class Program { static void Main(string[] args) { var path = @"C:\Users\Jano\Documents\thermopylae.txt"; using var fs = new FileStream(path, FileMode.Open, FileAccess.Read); using var sr = new StreamReader(fs, Encoding.UTF8); string line = String.Empty; while ((line = sr.ReadLine()) != null) { Console.WriteLine(line); } } } } ``` 該代碼示例逐行讀取文件。 ```cs string line = String.Empty; while ((line = streamReader.ReadLine()) != null) { Console.WriteLine(line); } ``` 在`while`循環中,我們使用`StreamReader`的`ReadLine()`方法逐行讀取文件的內容。 ## C# 與`StreamReader`的`ReadToEndAsync`異步讀取文本文件 `ReadToEndAsync()`方法異步讀取從當前位置到流末尾的所有字符,并將它們作為一個字符串返回。 `Program.cs` ```cs using System; using System.IO; using System.Text; using System.Threading.Tasks; namespace ReadTextFileAsync { class Program { static async Task Main(string[] args) { var path = @"C:\Users\Jano\Documents\thermopylae.txt"; using var fs = new FileStream(path, FileMode.Open, FileAccess.Read); using var sr = new StreamReader(fs, Encoding.UTF8); string content = await sr.ReadToEndAsync(); Console.WriteLine(content); } } } ``` 在下一個示例中,我們異步讀取文本文件。 ```cs static async Task Main(string[] args) ``` `async`修飾符允許在`Main()`方法中進行異步操作。 ```cs string content = await sr.ReadToEndAsync(); ``` `await`運算符應用于異步方法中的任務,以暫停該方法的執行,直到等待的任務完成。 在本文中,我們已經以各種方式在 C# 中閱讀了文本文件。 您可能也對以下相關教程感興趣: [MySQL C# 教程](/db/mysqlcsharptutorial/), [C# 中的日期和時間](/articles/csharpdatetime/),[用 C# 閱讀網頁](/csharp/readwebpage/)或 [C# Winforms 教程](/gui/csharpwinforms/)。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看