C# StreamWriter StreamWriter(String, Boolean, Encoding, Int32)
Description
StreamWriter StreamWriter(String, Boolean, Encoding, Int32)
Initializes a new instance of the StreamWriter class for the specified
file on the specified path, using the specified encoding and buffer size.
If the file exists, it can be either overwritten or appended to. If the file
does not exist, this constructor creates a new file.
Syntax
StreamWriter.StreamWriter(String, Boolean, Encoding, Int32)
has the following syntax.
public StreamWriter(
string path,/*from w w w . j a v a 2 s . c om*/
bool append,
Encoding encoding,
int bufferSize
)
Parameters
StreamWriter.StreamWriter(String, Boolean, Encoding, Int32)
has the following parameters.
path
- The complete file path to write to.append
- true to append data to the file; false to overwrite the file. If the specified file does not exist, this parameter has no effect, and the constructor creates a new file.encoding
- The character encoding to use.bufferSize
- The buffer size, in bytes.
Example
The following example demonstrates this constructor.
//from www .j a va2 s. c om
using System;
using System.IO;
using System.Text;
class Program
{
static void Main(string[] args)
{
string fileName = "test.txt";
string textToAdd = "java2s.com";
using (StreamWriter writer = new StreamWriter(fileName, true, Encoding.UTF8, 512))
{
writer.Write(textToAdd);
}
}
}