Home > Article > Backend Development > C# StreamWriter
To write the characters into a stream that follows a specific encoding, we make use of the class called StreamWriter class in C# and the method StreamWriter.Write() methods of StreamWriter class is responsible for writing characters into a stream. TextWriter class is the base class of StreamWriter class that is the StreamWriter class is inherited from the TextWriter class and this TextWriter class provides several methods which can be used to write an object to a string, writing strings to a file, to serialize XML, etc. and System.IO.namespace is the namespace in which the StreamWriter is defined and StreamWriter class provides several Write methods such as Write, WriteAsync, WriteLine, WriteLineAsync, etc.
The syntax of the StreamWriter class in C# is as follows:
public class StreamWriter : System.IO.TextWriter
Consider the below example to demonstrate the usage of StreamWriter to write data into a file:
Code:
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(); } } }
Output:
In the above program, a namespace called program is defined. Then a class called check is defined. Then the main method is called. Then the path of the file and the file name is assigned to a string variable. Then 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. Then data to be appended to the file is included. Then the instance of the stream writer class is closed after writing data to the File. Then data is read from the file by taking the path of the file as a parameter.
Program to demonstrate usage of StreamWriter class:
Code:
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 "); } } }
Output:
In the above program, a class called check is defined. Then the main method is called. Then an instance of stream writer class is created, and the path of the file is passed as a parameter to which the stream writer writes the data. Then the write() method of stream writer class is used to write the first line so that the next line continues from here. Then the writeline() method is used to write the second line and the next line starts from a new line. Then the writeline() method is used to write the third line and the next line starts from a new line. The output of the program is as shown in the snapshot above.
Program to demonstrate usage of StreamWriter class:
Code:
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."); } } }
Output:
In the above program, a class called check is defined. Then the main method is called. Then 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. Then a variable called plane is defined. Then an integer called high is defined. Then interpolation syntax in the string is used to make code efficient. The output of the program is as shown in the snapshot above.
In this tutorial, we understand the concept of StreamWriter class in C# through definition, the syntax of StreamWriter class in C#, working of StreamWriter class through programming examples and their outputs.
The above is the detailed content of C# StreamWriter. For more information, please follow other related articles on the PHP Chinese website!