為了將字元讀取到遵循特定編碼的流中,我們使用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
考慮下面的範例來解釋 StreamReader 從檔案中讀取資料的用法:
以下是c# StreamReader的範例
代碼:
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(); } } } }
輸出:
上面的程式中,程式就是定義的命名空間。然後檢查是否定義了類別。然後呼叫main方法。然後字串變數分配檔名和檔案路徑。然後建立字串編寫器類別實例,並將檔案路徑作為參數傳遞以將文字寫入檔案。然後包括要寫入文件的資料。然後將資料寫入檔案後關閉流編寫器類別實例。然後建立一個stream reader類別的實例,以檔案的路徑為參數從檔案中讀取資料。然後定義一個字串變數。然後一行一行地讀取資料。程式的輸出如上面的快照所示。
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(); } } }
輸出:
上面的程式中,程式就是定義的命名空間。然後檢查是否定義了類別。然後呼叫main方法。然後字串變數分配檔名和檔案路徑。然後建立字串編寫器類別實例,並將檔案路徑作為參數傳遞以將文字寫入檔案。然後使用流編寫器類別的 write() 方法寫入第一行,以便下一行從這裡繼續。然後使用writeline()方法寫入第二行,下一行另起一行。然後使用writeline()方法寫入第三行,下一行另起一行。然後建立一個stream reader類別的實例,以檔案的路徑為參數從檔案中讀取資料。然後定義一個字串變數。然後一行一行地讀取數據,直到行尾。程式的輸出如上面的快照所示。
以上是C# 流讀取器的詳細內容。更多資訊請關注PHP中文網其他相關文章!