📜  C#StreamWriter(1)

📅  最后修改于: 2023-12-03 14:40:28.477000             🧑  作者: Mango

C# StreamWriter

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.

Creating a StreamWriter object

To use the StreamWriter class, you first need to create an instance of it. This can be done using the following constructors:

1. 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");

2. 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);

3. 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);
Writing text to a file

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:

1. 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");

2. 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');

3. WriteLine()

This method writes a newline character to the output.

writer.WriteLine();
Flushing and closing the StreamWriter

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
}
Encoding

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);
Markdown code format:
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.