Trending October 2023 # Learn The Working Of Streamwriter Class In C# # Suggested November 2023 # Top 15 Popular | Vibergotobrazil.com

Trending October 2023 # Learn The Working Of Streamwriter Class In C# # Suggested November 2023 # Top 15 Popular

You are reading the article Learn The Working Of Streamwriter Class In C# updated in October 2023 on the website Vibergotobrazil.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested November 2023 Learn The Working Of Streamwriter Class In C#

Introduction to 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.

Web development, programming languages, Software testing & others

public class StreamWriter : System.IO.TextWriter Working of StreamWriter class in C#

Streams are used in file operations of C# to read data from the files and to write data into the files.

An extra layer that is created between the application and the file is called a stream.

The stream makes the file is being read smoothly and the data is written to the file smoothly.

The reason why the data from the files is broken into small chunks is that there is an impact on the performance of the application when the application tries to read the whole data from the file at once.

So, whenever data is to be written into a file, the data is first written to the stream, and then the data is written to the file from the stream.

Examples of C# StreamWriter

Consider the below example to demonstrate the usage of StreamWriter to write data into a file:

Example #1

Code:

using System; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace program { class check { static void Main(string[] args) { String pat = @"D:Ex.txt"; using (StreamWriter sw = File.AppendText(pat)) { sw.WriteLine("Welcome to StreamWriter class in C#"); sw.Close(); 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.

Example #2

Program to demonstrate usage of StreamWriter class:

using System.IO; class check { static void Main() { using (StreamWriter sw = new StreamWriter(@"D:imp.txt")) { sw.Write("Welcome to StreamWriter class in C# and "); sw.WriteLine("this program is demonstration of StreamWriter class in C# "); 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.

Example #3

Program to demonstrate usage of StreamWriter class:

using System.IO; class check { static void Main() { using (StreamWriter sw = new StreamWriter(@"D:Ex.txt")) { string plane = "Tejas"; int high = 120; 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.

Conclusion

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.

Recommended Articles

This is a guide to C# StreamWriter. Here we discuss the working of StreamWriter class in C# along with examples for better understanding. You may also look at the following articles to learn more –

You're reading Learn The Working Of Streamwriter Class In C#

Update the detailed information about Learn The Working Of Streamwriter Class In C# on the Vibergotobrazil.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!