首頁 >後端開發 >C#.Net教程 >C# 流讀取器

C# 流讀取器

WBOY
WBOY原創
2024-09-03 15:23:26435瀏覽

為了將字元讀取到遵循特定編碼的流中,我們使用C# 中稱為StreamReader 類的類,StreamReader 類的StreamWriter.Read() 方法負責讀取下一個字元或下一組流中的字符數。 TextReader類別是StreamReader類別的基底類,StreamReader類別繼承自TextReader類,這個TextReader類別提供了幾個可用於讀取字元、區塊、行等的方法,System.IO.namespace是定義StreamReader 的命名空間,StreamReader 類別提供了多種讀取方法,例如Peak、Read、ReadAsync、ReadBlock、ReadBlockAsync、ReadLine、ReadLineAsync、ReadToEnd、ReadToEndAsync 等

文法:

C#中StreamReader類別的語法如下:

public class StreamReader: System.IO.TextReader

C#中StreamReader類別的函數

  • 使用 C# 中的 Streams 從檔案中讀取資料。
  • 流是應用程式和檔案之間的額外層。
  • 使用串流可以順利讀取檔案中的資料。
  • 流接收從大檔案分解出來的小資料塊。應用程式可以從流中讀取這些小資料塊,而不必直接從較大的檔案中讀取所有資料
  • 如果應用程式嘗試從較大檔案讀取所有數​​據,則會影響應用程式的效能。
  • 因此,資料必須透過流讀取,而不是大檔案本身。

考慮下面的範例來解釋 StreamReader 從檔案中讀取資料的用法:

C# StreamReader 範例

以下是c# StreamReader的範例

範例#1

代碼:

using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//a namespace called program is defined
namespace program
{
//a class called check is defined
class check
{
//main method is called
static void Main(string[] args)
{
//the path of the file and the file name is assigned to a string variable
String def = @"D:\imp.txt";
//an instance of the string writer class is created, and the path of the file is passed as a parameter to append text to the file
using (StreamWriter stwr = File.AppendText(def))
{
//data to be appended to the file is included
stwr.WriteLine("Welcome to StreamWriter class in C#");
//the instance of the streamwriter class is closed after writing data to the File
stwr.Close();
try
{
// an instance of stream reader class is created, and data is read from the file by taking the path of the file as parameter
using (StreamReader read = new StreamReader(def))
{
//a string variable is defined
string line1;
// Data is being read one line after the other
while ((line1 = read.ReadLine()) != null)
{
Console.WriteLine(line1);
}
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
Console.ReadKey();
}
}
}
}

輸出:

C# 流讀取器

上面的程式中,程式就是定義的命名空間。然後檢查是否定義了類別。然後呼叫main方法。然後字串變數分配檔名和檔案路徑。然後建立字串編寫器類別實例,並將檔案路徑作為參數傳遞以將文字寫入檔案。然後包括要寫入文件的資料。然後將資料寫入檔案後關閉流編寫器類別實例。然後建立一個stream reader類別的實例,以檔案的路徑為參數從檔案中讀取資料。然後定義一個字串變數。然後一行一行地讀取資料。程式的輸出如上面的快照所示。

範例#2

C#程式講解StreamReader類別的用法:

代碼:

using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//a class called check is defined
class check
{
//main method is called
static void Main()
{
String fin = @"D:\Ex.txt"
//an instance of streamwriter class is created and the path of the file is passed as a parameter
using (StreamWriter stwf = new StreamWriter(fin))
{
//write() method of stream writer class is used to write the first line so that the next line continues from here
stwf.Write("Welcome to StreamWriter class in C# and ");
//writeline() method is used to write the second line and the next line starts from a new line
stwf.WriteLine("this program is demonstration of StreamWriter class in C# ");
//writeline() method is used to write the third line and the next line starts from a new line
stwf.WriteLine("I hope you are learning ");
stwf.Dispose();
try
{
// an instance of stream reader class is created, and data is read from the file by taking the path of the file as parameter
using (StreamReader read = new StreamReader(fin))
{
//a string variable is defined
string line1;
// Data is being read one line after the other
while ((line1 = read.ReadLine()) != null)
{
Console.WriteLine(line1);
}
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
Console.ReadKey();
}
}
}

輸出:

C# 流讀取器

上面的程式中,程式就是定義的命名空間。然後檢查是否定義了類別。然後呼叫main方法。然後字串變數分配檔名和檔案路徑。然後建立字串編寫器類別實例,並將檔案路徑作為參數傳遞以將文字寫入檔案。然後使用流編寫器類別的 write() 方法寫入第一行,以便下一行從這裡繼續。然後使用writeline()方法寫入第二行,下一行另起一行。然後使用writeline()方法寫入第三行,下一行另起一行。然後建立一個stream reader類別的實例,以檔案的路徑為參數從檔案中讀取資料。然後定義一個字串變數。然後一行一行地讀取數據,直到行尾。程式的輸出如上面的快照所示。

以上是C# 流讀取器的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
上一篇:C# 字串編寫器下一篇:C# 字串編寫器