C# StreamWriter

WBOY
Release: 2024-09-03 15:23:32
Original
682 people have browsed it

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
Copy after login

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 data from the large files are broken down into small chunks and then sent to the stream. Then the application reads these chunks of data from the stream rather than trying to read the whole data at once. This is the advantage of using streams.
  • 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;
//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();
}
}
}
Copy after login

Output:

C# StreamWriter

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:

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 ");
}
}
}
Copy after login

Output:

C# StreamWriter

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:

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.");
}
}
}
Copy after login

Output:

C# StreamWriter

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.

The above is the detailed content of C# StreamWriter. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template