Export (0) Print
Expand All

StreamWriter Constructor

.NET Framework 1.1

Initializes a new instance of the StreamWriter class.

Overload List

Initializes a new instance of the StreamWriter class for the specified stream, using the default encoding and buffer size.

Supported by the .NET Compact Framework.

[Visual Basic] Public Sub New(Stream)
[C#] public StreamWriter(Stream);
[C++] public: StreamWriter(Stream*);
[JScript] public function StreamWriter(Stream);

Initializes a new instance of the StreamWriter class for the specified file on the specified path, using the default encoding and buffer size.

Supported by the .NET Compact Framework.

[Visual Basic] Public Sub New(String)
[C#] public StreamWriter(string);
[C++] public: StreamWriter(String*);
[JScript] public function StreamWriter(String);

Initializes a new instance of the StreamWriter class for the specified stream, using the specified encoding and the default buffer size.

Supported by the .NET Compact Framework.

[Visual Basic] Public Sub New(Stream, Encoding)
[C#] public StreamWriter(Stream, Encoding);
[C++] public: StreamWriter(Stream*, Encoding*);
[JScript] public function StreamWriter(Stream, Encoding);

Initializes a new instance of the StreamWriter class for the specified file on the specified path, using the default 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.

Supported by the .NET Compact Framework.

[Visual Basic] Public Sub New(String, Boolean)
[C#] public StreamWriter(string, bool);
[C++] public: StreamWriter(String*, bool);
[JScript] public function StreamWriter(String, Boolean);

Initializes a new instance of the StreamWriter class for the specified stream, using the specified encoding and buffer size.

Supported by the .NET Compact Framework.

[Visual Basic] Public Sub New(Stream, Encoding, Integer)
[C#] public StreamWriter(Stream, Encoding, int);
[C++] public: StreamWriter(Stream*, Encoding*, int);
[JScript] public function StreamWriter(Stream, Encoding, int);

Initializes a new instance of the StreamWriter class for the specified file on the specified path, using the specified encoding and default 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.

Supported by the .NET Compact Framework.

[Visual Basic] Public Sub New(String, Boolean, Encoding)
[C#] public StreamWriter(string, bool, Encoding);
[C++] public: StreamWriter(String*, bool, Encoding*);
[JScript] public function StreamWriter(String, Boolean, Encoding);

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.

Supported by the .NET Compact Framework.

[Visual Basic] Public Sub New(String, Boolean, Encoding, Integer)
[C#] public StreamWriter(string, bool, Encoding, int);
[C++] public: StreamWriter(String*, bool, Encoding*, int);
[JScript] public function StreamWriter(String, Boolean, Encoding, int);

Example

[Visual Basic, C#, C++] The following example demonstrates the StreamWriter constructors.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of the StreamWriter constructor. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
Public Sub CreateTextFile(ByVal FileName As String, _
    ByVal TextToAdd As String)

    Dim Fs As FileStream = New FileStream(FileName, _
        FileMode.CreateNew, FileAccess.Write, FileShare.None)

    Dim SwFromFile As StreamWriter = New StreamWriter(FileName)
    SwFromFile.Write(TextToAdd)
    SwFromFile.Flush()
    SwFromFile.Close()

    Dim SwFromFileStream As StreamWriter = New StreamWriter(Fs)
    SwFromFileStream.Write(TextToAdd)
    SwFromFileStream.Flush()
    SwFromFileStream.Close()

    Dim SwFromFileStreamDefaultEnc As StreamWriter = _
     New StreamWriter(Fs, System.Text.Encoding.Default)
    SwFromFileStreamDefaultEnc.Write(TextToAdd)
    SwFromFileStreamDefaultEnc.Flush()
    SwFromFileStreamDefaultEnc.Close()

    Dim SwFromFileTrue As StreamWriter = _
     New StreamWriter(FileName, True)
    SwFromFileTrue.Write(TextToAdd)
    SwFromFileTrue.Flush()
    SwFromFileTrue.Close()

    Dim SwFromFileTrueUTF8Buffer = _
     New StreamWriter(FileName, _
     True, System.Text.Encoding.UTF8, 512)
    SwFromFileTrueUTF8Buffer.Write(TextToAdd)
    SwFromFileTrueUTF8Buffer.Flush()
    SwFromFileTrueUTF8Buffer.Close()

    Dim SwFromFileTrueUTF8 = _
     New StreamWriter(FileName, True, _
     System.Text.Encoding.UTF8)
    SwFromFileTrueUTF8.Write(TextToAdd)
    SwFromFileTrueUTF8.Flush()
    SwFromFileTrueUTF8.Close()

    Dim SwFromFileStreamUTF8Buffer = _
     New StreamWriter(Fs, System.Text.Encoding.UTF8, 512)
    SwFromFileStreamUTF8Buffer.Write(textToAdd)
    SwFromFileStreamUTF8Buffer.Flush()
    SwFromFileStreamUTF8Buffer.Close()

End Sub

[C#] 
public void CreateTextFile(string fileName, string textToAdd) 
{
    string logFile = DateTime.Now.ToShortDateString()
        .Replace(@"/",@"-").Replace(@"\",@"-") + ".log";
    
    FileStream fs = new FileStream(fileName,
        FileMode.CreateNew, FileAccess.Write, FileShare.None);
    
    StreamWriter swFromFile = new StreamWriter(logFile);
    swFromFile.Write(textToAdd);
    swFromFile.Flush();
    swFromFile.Close();

    StreamWriter swFromFileStream = new StreamWriter(fs);
    swFromFileStream.Write(textToAdd);
    swFromFileStream.Flush();
    swFromFileStream.Close();

    StreamWriter swFromFileStreamDefaultEnc = 
        new System.IO.StreamWriter(fs, 
        System.Text.Encoding.Default);
    swFromFileStreamDefaultEnc.Write(textToAdd);
    swFromFileStreamDefaultEnc.Flush();
    swFromFileStreamDefaultEnc.Close();

    StreamWriter swFromFileTrue = 
        new StreamWriter(fileName,true);
    swFromFileTrue.Write(textToAdd);
    swFromFileTrue.Flush();
    swFromFileTrue.Close();
    
    StreamWriter swFromFileTrueUTF8Buffer = 
        new StreamWriter(fileName, 
        true, System.Text.Encoding.UTF8,512);
    swFromFileTrueUTF8Buffer.Write(textToAdd);
    swFromFileTrueUTF8Buffer.Flush();
    swFromFileTrueUTF8Buffer.Close();

    StreamWriter swFromFileTrueUTF8 = 
        new StreamWriter(fileName, true,
        System.Text.Encoding.UTF8);
    swFromFileTrueUTF8.Write(textToAdd);
    swFromFileTrueUTF8.Flush();
    swFromFileTrueUTF8.Close();

    StreamWriter swFromFileStreamUTF8Buffer = 
        new StreamWriter(fs, System.Text.Encoding.UTF8, 512);
    swFromFileStreamUTF8Buffer.Write(textToAdd);
    swFromFileStreamUTF8Buffer.Flush();
    swFromFileStreamUTF8Buffer.Close();
}

[C++] 
public:
   void CreateTextFile(String* fileName, String* textToAdd) 
   {
      String* logFile = String::Concat(
         DateTime::Now.ToShortDateString()->Replace(S"/",S"-")->Replace(S"\\",S"-"),
         S".log" );

      FileStream* fs =
         new FileStream(fileName, FileMode::CreateNew, FileAccess::Write, FileShare::None);

      StreamWriter* swFromFile = new StreamWriter(logFile);
      swFromFile->Write(textToAdd);
      swFromFile->Flush();
      swFromFile->Close();

      StreamWriter* swFromFileStream = new StreamWriter(fs);
      swFromFileStream->Write(textToAdd);
      swFromFileStream->Flush();
      swFromFileStream->Close();

      StreamWriter* swFromFileStreamDefaultEnc = 
         new System::IO::StreamWriter(fs, System::Text::Encoding::Default);
      swFromFileStreamDefaultEnc->Write(textToAdd);
      swFromFileStreamDefaultEnc->Flush();
      swFromFileStreamDefaultEnc->Close();

      StreamWriter* swFromFileTrue = new StreamWriter(fileName,true);
      swFromFileTrue->Write(textToAdd);
      swFromFileTrue->Flush();
      swFromFileTrue->Close();

      StreamWriter* swFromFileTrueUTF8Buffer = 
         new StreamWriter(fileName, true, System::Text::Encoding::UTF8,512);
      swFromFileTrueUTF8Buffer->Write(textToAdd);
      swFromFileTrueUTF8Buffer->Flush();
      swFromFileTrueUTF8Buffer->Close();

      StreamWriter* swFromFileTrueUTF8 = 
         new StreamWriter(fileName, true,System::Text::Encoding::UTF8);
      swFromFileTrueUTF8->Write(textToAdd);
      swFromFileTrueUTF8->Flush();
      swFromFileTrueUTF8->Close();

      StreamWriter* swFromFileStreamUTF8Buffer = 
         new StreamWriter(fs, System::Text::Encoding::UTF8, 512);
      swFromFileStreamUTF8Buffer->Write(textToAdd);
      swFromFileStreamUTF8Buffer->Flush();
      swFromFileStreamUTF8Buffer->Close();
   }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

StreamWriter Class | StreamWriter Members | System.IO Namespace

Show:
© 2014 Microsoft