首頁  >  文章  >  後端開發  >  C# StreamWriter

C# StreamWriter

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

為了將字元寫入遵循特定編碼的流中,我們使用 C# 中稱為 StreamWriter 類別的類,StreamWriter 類別的 StreamWriter.Write() 方法負責將字元寫入流中。 TextWriter類別是StreamWriter類別的基類,StreamWriter類別繼承自TextWriter類,而TextWriter類別提供了多種方法,可用於將物件寫入字串、將字串寫入檔案、序列化XML等. 而System.IO .namespace 是定義StreamWriter 的命名空間,StreamWriter 類別提供了Write、WriteAsync、WriteLine、WriteLineAsync 等多種Write 方法

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

public class StreamWriter : System.IO.TextWriter

C# 中 StreamWriter 類別的工作

  • C# 的檔案操作中使用流來從檔案中讀取資料以及將資料寫入檔案中。
  • 在應用程式和檔案之間建立的額外層稱為流。
  • 流讓檔案讀取順利,資料寫入檔案順利。
  • 來自大檔案的資料被分解成小塊,然後發送到流。然後,應用程式從流中讀取這些資料塊,而不是嘗試立即讀取整個資料。這就是使用流的優點。
  • 檔案中的資料被分成小塊的原因是,當應用程式嘗試一次從檔案中讀取整個資料時,會對應用程式的效能產生影響。
  • 所以,每當要寫入資料到檔案時,都會先將資料寫入流,然後再將資料從流寫入檔案。

C# StreamWriter 範例

考慮以下範例來示範如何使用 StreamWriter 將資料寫入檔案:

範例#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 pat = @"D:\Ex.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 sw = File.AppendText(pat))
{
//data to be appended to the file is included
sw.WriteLine("Welcome to StreamWriter class in C#");
//the instance of the streamwriter class is closed after writing data to the File
sw.Close();
//data is read from the file by taking the path of the file as parameter
Console.WriteLine(File.ReadAllText(pat));
}
Console.ReadKey();
}
}
}

輸出:

C# StreamWriter

在上面的程式中,定義了一個名為program的命名空間。然後定義一個名為check的類別。然後呼叫main方法。然後將檔案的路徑和檔案名稱指派給一個字串變數。然後建立字串編寫器類別的實例,並將檔案的路徑作為參數傳遞以將文字附加到檔案中。然後包括要附加到文件的資料。然後將資料寫入檔案後關閉流寫入器類別的實例。然後以文件的路徑為參數從文件中讀取資料。

範例#2

示範 StreamWriter 類別用法的程式:

代碼:

using System.IO;
//a class called check is defined
class check
{
//main method is called
static void Main()
{
//an instance of streamwriter class is created and the path of the file is passed as a parameter
using (StreamWriter sw = new StreamWriter(@"D:\imp.txt"))
{
//write() method of stream writer class is used to write the first line so that the next line continues from here
sw.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
sw.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
sw.WriteLine("I hope you are learning ");
}
}
}

輸出:

C# StreamWriter

在上面的程式中,定義了一個名為check的類別。然後呼叫main方法。然後建立流寫入器類別的實例,並將檔案的路徑作為參數傳遞給流寫入器寫入資料。然後使用流編寫器類別的 write() 方法寫入第一行,以便下一行從這裡繼續。然後使用writeline()方法寫入第二行,下一行另起一行。然後使用writeline()方法寫入第三行,下一行另起一行。程式的輸出如上面的快照所示。

範例 #3

示範 StreamWriter 類別用法的程式:

代碼:

using System.IO;
//a class called check is defined
class check
{
//main method is called
static void Main()
{
//an instance of the stream writer class is created and the path of the file to which the data must be written is passed as a parameter
using (StreamWriter sw = new StreamWriter(@"D:\Ex.txt"))
{
//a variable called plane is defined
string plane = "Tejas";
//an integer called high is defined
int high = 120;
//interpolation syntax in string is used to make code efficient.
sw.WriteLine($"The plane {plane} flies {high} feet high.");
}
}
}

輸出:

C# StreamWriter

在上面的程式中,定義了一個名為check的類別。然後呼叫main方法。然後建立流編寫器類別的實例,並將必須寫入資料的檔案的路徑作為參數傳遞。然後定義一個名為plane的變數。然後定義一個稱為high的整數。然後使用字串中的插值語法來提高程式碼效率。程式的輸出如上面的快照所示。

結論

在本教程中,我們透過定義來了解 C# 中 StreamWriter 類別的概念、C# 中 StreamWriter 類別的語法、透過程式設計範例及其輸出來了解 StreamWriter 類別的工作原理。

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

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