目錄
C# 中 StreamWriter 類別的工作
C# StreamWriter 範例
範例 #3
結論
首頁 後端開發 C#.Net教程 C# StreamWriter

C# StreamWriter

Sep 03, 2024 pm 03:23 PM
c# c# tutorial

為了將字元寫入遵循特定編碼的流中,我們使用 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

熱AI工具

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

PHP教程
1517
276
c#多線程和異步的區別 c#多線程和異步的區別 Apr 03, 2025 pm 02:57 PM

多線程和異步的區別在於,多線程同時執行多個線程,而異步在不阻塞當前線程的情況下執行操作。多線程用於計算密集型任務,而異步用於用戶交互操作。多線程的優勢是提高計算性能,異步的優勢是不阻塞 UI 線程。選擇多線程還是異步取決於任務性質:計算密集型任務使用多線程,與外部資源交互且需要保持 UI 響應的任務使用異步。

C#與C:歷史,進化和未來前景 C#與C:歷史,進化和未來前景 Apr 19, 2025 am 12:07 AM

C#和C 的歷史與演變各有特色,未來前景也不同。 1.C 由BjarneStroustrup在1983年發明,旨在將面向對象編程引入C語言,其演變歷程包括多次標準化,如C 11引入auto關鍵字和lambda表達式,C 20引入概念和協程,未來將專注於性能和系統級編程。 2.C#由微軟在2000年發布,結合C 和Java的優點,其演變注重簡潔性和生產力,如C#2.0引入泛型,C#5.0引入異步編程,未來將專注於開發者的生產力和雲計算。

c#多線程編程是什麼  c#多線程編程用處 c#多線程編程是什麼 c#多線程編程用處 Apr 03, 2025 pm 02:45 PM

C# 多線程編程是一種讓程序同時執行多項任務的技術,它可以通過提升性能、提高響應能力和實現並行處理來提高程序效率。雖然 Thread 類提供了直接創建線程的方法,但 Task 和 async/await 等高級工具可以提供更安全的異步操作和更簡潔的代碼結構。多線程編程中常見的難題包括死鎖、競態條件和資源洩漏,需要仔細設計線程模型和使用適當的同步機制來避免這些問題。

C#.NET:使用.NET生態系統構建應用程序 C#.NET:使用.NET生態系統構建應用程序 Apr 27, 2025 am 12:12 AM

如何利用.NET構建應用?使用.NET構建應用可以通過以下步驟實現:1)了解.NET基礎知識,包括C#語言和跨平台開發支持;2)學習核心概念,如.NET生態系統的組件和工作原理;3)掌握基本和高級用法,從簡單控制台應用到復雜的WebAPI和數據庫操作;4)熟悉常見錯誤與調試技巧,如配置和數據庫連接問題;5)應用性能優化與最佳實踐,如異步編程和緩存。

從網絡到桌面:C#.NET的多功能性 從網絡到桌面:C#.NET的多功能性 Apr 15, 2025 am 12:07 AM

C#.NETisversatileforbothwebanddesktopdevelopment.1)Forweb,useASP.NETfordynamicapplications.2)Fordesktop,employWindowsFormsorWPFforrichinterfaces.3)UseXamarinforcross-platformdevelopment,enablingcodesharingacrossWindows,macOS,Linux,andmobiledevices.

c#多線程的好處有哪些 c#多線程的好處有哪些 Apr 03, 2025 pm 02:51 PM

多線程的好處在於能提升性能和資源利用率,尤其適用於處理大量數據或執行耗時操作。它允許同時執行多個任務,提高效率。然而,線程過多會導致性能下降,因此需要根據 CPU 核心數和任務特性謹慎選擇線程數。另外,多線程編程涉及死鎖和競態條件等挑戰,需要使用同步機制解決,需要具備紮實的並發編程知識,權衡利弊並謹慎使用。

.NET框架與C#:解碼術語 .NET框架與C#:解碼術語 Apr 21, 2025 am 12:05 AM

.NETFramework是一個軟件框架,C#是一種編程語言。 1..NETFramework提供庫和服務,支持桌面、Web和移動應用開發。 2.C#設計用於.NETFramework,支持現代編程功能。 3..NETFramework通過CLR管理代碼執行,C#代碼編譯成IL後由CLR運行。 4.使用.NETFramework可快速開發應用,C#提供如LINQ的高級功能。 5.常見錯誤包括類型轉換和異步編程死鎖,調試需用VisualStudio工具。

將C#.NET應用程序部署到Azure/AWS:逐步指南 將C#.NET應用程序部署到Azure/AWS:逐步指南 Apr 23, 2025 am 12:06 AM

如何將C#.NET應用部署到Azure或AWS?答案是使用AzureAppService和AWSElasticBeanstalk。 1.在Azure上,使用AzureAppService和AzurePipelines自動化部署。 2.在AWS上,使用AmazonElasticBeanstalk和AWSLambda實現部署和無服務器計算。

See all articles