📅  最后修改于: 2023-12-03 14:40:28.477000             🧑  作者: Mango
The StreamWriter
is a class in C# that provides a way to write text to a file or stream in a specified encoding. It is part of the System.IO
namespace and is commonly used for writing output to files, logs, or network streams.
To use the StreamWriter
class, you first need to create an instance of it. This can be done using the following constructors:
StreamWriter(String path)
This constructor creates a StreamWriter
object that writes to a file specified by the given path. If the file already exists, it will be overwritten. If the file doesn't exist, a new file will be created.
StreamWriter writer = new StreamWriter("path/to/file.txt");
StreamWriter(String path, Boolean append)
This constructor is similar to the previous one, but it allows you to specify whether the output should be appended to an existing file instead of overwriting it. If append
is set to true
, the text will be appended to the end of the file.
StreamWriter writer = new StreamWriter("path/to/file.txt", true);
StreamWriter(Stream stream)
This constructor creates a StreamWriter
object that writes to the specified stream. It can be used to write to network streams, memory streams, or any other type of stream.
StreamWriter writer = new StreamWriter(stream);
Once you have created a StreamWriter
object, you can use its various methods to write text to a file or stream. Some commonly used methods are:
Write(String value)
and WriteLine(String value)
These methods are used to write a string to the output. The WriteLine
method adds a newline character at the end of the string.
writer.Write("Hello");
writer.WriteLine("World");
Write(char value)
and WriteLine(char value)
These methods are used to write a single character to the output. The WriteLine
method adds a newline character after the character.
writer.Write('A');
writer.WriteLine('B');
WriteLine()
This method writes a newline character to the output.
writer.WriteLine();
To ensure that all the text is written to the file or stream, you need to call the Flush()
method. This will clear the internal buffer and write any pending output.
writer.Flush();
After you have finished writing, it is important to close the StreamWriter
to release any resources it is holding. This can be done using the Close()
method or by wrapping the StreamWriter
in a using
statement, which automatically handles the closing.
writer.Close();
// or
using (StreamWriter writer = new StreamWriter("path/to/file.txt"))
{
// Write text to the file
}
By default, the StreamWriter
uses UTF-8 encoding to write text. If you want to use a different encoding, you can pass an instance of the Encoding
class in the constructor.
StreamWriter writer = new StreamWriter("path/to/file.txt", false, Encoding.ASCII);
StreamWriter writer = new StreamWriter("path/to/file.txt");
writer.Write("Hello");
writer.WriteLine("World");
writer.Write('A');
writer.WriteLine('B');
writer.WriteLine();
writer.Flush();
writer.Close();
This is a comprehensive introduction to the StreamWriter
class in C#. It provides a convenient way to write text to files or streams and can be a valuable tool for programmers working with text-based output.