Uredi

Deli z drugimi prek


StreamWriter Class

Definition

Implements a TextWriter for writing characters to a stream in a particular encoding.

public ref class StreamWriter : System::IO::TextWriter
public class StreamWriter : System.IO.TextWriter
[System.Serializable]
public class StreamWriter : System.IO.TextWriter
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public class StreamWriter : System.IO.TextWriter
type StreamWriter = class
    inherit TextWriter
[<System.Serializable>]
type StreamWriter = class
    inherit TextWriter
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type StreamWriter = class
    inherit TextWriter
Public Class StreamWriter
Inherits TextWriter
Inheritance
StreamWriter
Inheritance
Attributes

Examples

The following example shows how to use a StreamWriter object to write a file that lists the directories on the C drive, and then uses a StreamReader object to read and display each directory name. A good practice is to use these objects in a using statement so that the unmanaged resources are correctly disposed. The using statement automatically calls Dispose on the object when the code that is using it has completed. The constructor used in this example is not supported for use in Windows Store Apps.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace StreamReadWrite
{
    class Program
    {
        static void Main(string[] args)
        {
            // Get the directories currently on the C drive.
            DirectoryInfo[] cDirs = new DirectoryInfo(@"c:\").GetDirectories();

            // Write each directory name to a file.
            using (StreamWriter sw = new StreamWriter("CDriveDirs.txt"))
            {
                foreach (DirectoryInfo dir in cDirs)
                {
                    sw.WriteLine(dir.Name);
                }
            }

            // Read and show each line from the file.
            string line = "";
            using (StreamReader sr = new StreamReader("CDriveDirs.txt"))
            {
                while ((line = sr.ReadLine()) != null)
                {
                    Console.WriteLine(line);
                }
            }
        }
    }
}
Imports System.IO
Class Program

    Shared Sub Main()

        ' Get the directories currently on the C drive.
        Dim cDirs As DirectoryInfo() = New DirectoryInfo("c:\").GetDirectories()

        ' Write each directory name to a file.
        Using sw As StreamWriter = New StreamWriter("CDriveDirs.txt")
            For Each Dir As DirectoryInfo In cDirs
                sw.WriteLine(Dir.Name)
            Next
        End Using

        'Read and show each line from the file.
        Dim line As String = ""
        Using sr As StreamReader = New StreamReader("CDriveDirs.txt")
            Do
                line = sr.ReadLine()
                Console.WriteLine(line)
            Loop Until line Is Nothing
        End Using


    End Sub

End Class

Remarks

StreamWriter is designed for character output in a particular encoding, whereas classes derived from Stream are designed for byte input and output.

Important

This type implements the IDisposable interface. When you have finished using the type, you should dispose of it either directly or indirectly. To dispose of the type directly, call its Dispose method in a try/catch block. To dispose of it indirectly, use a language construct such as using (in C#) or Using (in Visual Basic). For more information, see the "Using an Object that Implements IDisposable" section in the IDisposable interface topic.

StreamWriter defaults to using an instance of UTF8Encoding unless specified otherwise. This instance of UTF8Encoding is constructed without a byte order mark (BOM), so its GetPreamble method returns an empty byte array. The default UTF-8 encoding for this constructor throws an exception on invalid bytes. This behavior is different from the behavior provided by the encoding object in the Encoding.UTF8 property. To specify a BOM and determine whether an exception is thrown on invalid bytes, use a constructor that accepts an encoding object as a parameter, such as StreamWriter(String, Boolean, Encoding) or StreamWriter.

By default, a StreamWriter is not thread safe. See TextWriter.Synchronized for a thread-safe wrapper.

For a list of common I/O tasks, see Common I/O Tasks.

Constructors

StreamWriter(Stream, Encoding, Int32, Boolean)

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

StreamWriter(Stream, Encoding, Int32)

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

StreamWriter(Stream, Encoding)

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

StreamWriter(Stream)

Initializes a new instance of the StreamWriter class for the specified stream by using UTF-8 encoding and the default buffer size.

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.

StreamWriter(String, Boolean, Encoding)

Initializes a new instance of the StreamWriter class for the specified file by 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.

StreamWriter(String, Boolean)

Initializes a new instance of the StreamWriter class for the specified file by 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.

StreamWriter(String, Encoding, FileStreamOptions)

Initializes a new instance of the StreamWriter class for the specified file, using the specified encoding, and configured with the specified FileStreamOptions object.

StreamWriter(String, FileStreamOptions)

Initializes a new instance of the StreamWriter class for the specified file, using the default encoding, and configured with the specified FileStreamOptions object.

StreamWriter(String)

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

Fields

CoreNewLine

Stores the newline characters used for this TextWriter.

(Inherited from TextWriter)
Null

Provides a StreamWriter with no backing store that can be written to, but not read from.

Properties

AutoFlush

Gets or sets a value indicating whether the StreamWriter will flush its buffer to the underlying stream after every call to Write(Char).

BaseStream

Gets the underlying stream that interfaces with a backing store.

Encoding

Gets the Encoding in which the output is written.

FormatProvider

Gets an object that controls formatting.

(Inherited from TextWriter)
NewLine

Gets or sets the line terminator string used by the current TextWriter.

(Inherited from TextWriter)

Methods

Close()

Closes the current StreamWriter object and the underlying stream.

Close()

Closes the current writer and releases any system resources associated with the writer.

(Inherited from TextWriter)
CreateObjRef(Type)

Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.

(Inherited from MarshalByRefObject)
Dispose()

Releases all resources used by the TextWriter object.

(Inherited from TextWriter)
Dispose(Boolean)

Causes any buffered data to be written to the underlying stream, releases the unmanaged resources used by the StreamWriter, and optionally the managed resources.

DisposeAsync()

Asynchronously writes any buffered data to the underlying stream and releases the unmanaged resources used by the StreamWriter.

DisposeAsync()

Asynchronously releases all resources used by the TextWriter object.

(Inherited from TextWriter)
Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
Finalize()

Frees the resources of the current StreamWriter before it is reclaimed by the garbage collector.

Flush()

Clears all buffers for the current writer and causes any buffered data to be written to the underlying stream.

FlushAsync()

Clears all buffers for this stream asynchronously and causes any buffered data to be written to the underlying device.

FlushAsync()

Asynchronously clears all buffers for the current writer and causes any buffered data to be written to the underlying device.

(Inherited from TextWriter)
FlushAsync(CancellationToken)

Clears all buffers for this stream asynchronously and causes any buffered data to be written to the underlying device.

FlushAsync(CancellationToken)

Asynchronously clears all buffers for the current writer and causes any buffered data to be written to the underlying device.

(Inherited from TextWriter)
GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetLifetimeService()
Obsolete.

Retrieves the current lifetime service object that controls the lifetime policy for this instance.

(Inherited from MarshalByRefObject)
GetType()

Gets the Type of the current instance.

(Inherited from Object)
InitializeLifetimeService()
Obsolete.

Obtains a lifetime service object to control the lifetime policy for this instance.

(Inherited from MarshalByRefObject)
MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
MemberwiseClone(Boolean)

Creates a shallow copy of the current MarshalByRefObject object.

(Inherited from MarshalByRefObject)
ToString()

Returns a string that represents the current object.

(Inherited from Object)
Write(Boolean)

Writes the text representation of a Boolean value to the text stream.

(Inherited from TextWriter)
Write(Char)

Writes a character to the stream.

Write(Char[], Int32, Int32)

Writes a subarray of characters to the stream.

Write(Char[])

Writes a character array to the stream.

Write(Decimal)

Writes the text representation of a decimal value to the text stream.

(Inherited from TextWriter)
Write(Double)

Writes the text representation of an 8-byte floating-point value to the text stream.

(Inherited from TextWriter)
Write(Int32)

Writes the text representation of a 4-byte signed integer to the text stream.

(Inherited from TextWriter)
Write(Int64)

Writes the text representation of an 8-byte signed integer to the text stream.

(Inherited from TextWriter)
Write(Object)

Writes the text representation of an object to the text stream by calling the ToString method on that object.

(Inherited from TextWriter)
Write(ReadOnlySpan<Char>)

Writes a character span to the stream.

Write(ReadOnlySpan<Char>)

Writes a character span to the text stream.

(Inherited from TextWriter)
Write(Single)

Writes the text representation of a 4-byte floating-point value to the text stream.

(Inherited from TextWriter)
Write(String, Object, Object, Object)

Writes a formatted string to the stream, using the same semantics as the Format(String, Object, Object, Object) method.

Write(String, Object, Object, Object)

Writes a formatted string to the text stream, using the same semantics as the Format(String, Object, Object, Object) method.

(Inherited from TextWriter)
Write(String, Object, Object)

Writes a formatted string to the stream using the same semantics as the Format(String, Object, Object) method.

Write(String, Object, Object)

Writes a formatted string to the text stream using the same semantics as the Format(String, Object, Object) method.

(Inherited from TextWriter)
Write(String, Object)

Writes a formatted string to the stream, using the same semantics as the Format(String, Object) method.

Write(String, Object)

Writes a formatted string to the text stream, using the same semantics as the Format(String, Object) method.

(Inherited from TextWriter)
Write(String, Object[])

Writes a formatted string to the stream, using the same semantics as the Format(String, Object[]) method.

Write(String, Object[])

Writes a formatted string to the text stream, using the same semantics as the Format(String, Object[]) method.

(Inherited from TextWriter)
Write(String, ReadOnlySpan<Object>)

Writes a formatted string to the stream, using the same semantics as Format(String, ReadOnlySpan<Object>).

Write(String, ReadOnlySpan<Object>)

Writes a formatted string to the text stream, using the same semantics as Format(String, ReadOnlySpan<Object>).

(Inherited from TextWriter)
Write(String)

Writes a string to the stream.

Write(StringBuilder)

Writes a string builder to the text stream.

(Inherited from TextWriter)
Write(UInt32)

Writes the text representation of a 4-byte unsigned integer to the text stream.

(Inherited from TextWriter)
Write(UInt64)

Writes the text representation of an 8-byte unsigned integer to the text stream.

(Inherited from TextWriter)
WriteAsync(Char)

Asynchronously writes a character to the stream.

WriteAsync(Char)

Writes a character to the text stream asynchronously.

(Inherited from TextWriter)
WriteAsync(Char[], Int32, Int32)

Asynchronously writes a subarray of characters to the stream.

WriteAsync(Char[], Int32, Int32)

Writes a subarray of characters to the text stream asynchronously.

(Inherited from TextWriter)
WriteAsync(Char[])

Writes a character array to the text stream asynchronously.

(Inherited from TextWriter)
WriteAsync(ReadOnlyMemory<Char>, CancellationToken)

Asynchronously writes a character memory region to the stream.

WriteAsync(ReadOnlyMemory<Char>, CancellationToken)

Asynchronously writes a character memory region to the text stream.

(Inherited from TextWriter)
WriteAsync(String)

Asynchronously writes a string to the stream.

WriteAsync(String)

Writes a string to the text stream asynchronously.

(Inherited from TextWriter)
WriteAsync(StringBuilder, CancellationToken)

Asynchronously writes a string builder to the text stream.

(Inherited from TextWriter)
WriteLine()

Writes a line terminator to the text stream.

(Inherited from TextWriter)
WriteLine(Boolean)

Writes the text representation of a Boolean value to the text stream, followed by a line terminator.

(Inherited from TextWriter)
WriteLine(Char)

Writes a character to the text stream, followed by a line terminator.

(Inherited from TextWriter)
WriteLine(Char[], Int32, Int32)

Writes a subarray of characters to the text stream, followed by a line terminator.

(Inherited from TextWriter)
WriteLine(Char[])

Writes an array of characters to the text stream, followed by a line terminator.

(Inherited from TextWriter)
WriteLine(Decimal)

Writes the text representation of a decimal value to the text stream, followed by a line terminator.

(Inherited from TextWriter)
WriteLine(Double)

Writes the text representation of a 8-byte floating-point value to the text stream, followed by a line terminator.

(Inherited from TextWriter)
WriteLine(Int32)

Writes the text representation of a 4-byte signed integer to the text stream, followed by a line terminator.

(Inherited from TextWriter)
WriteLine(Int64)

Writes the text representation of an 8-byte signed integer to the text stream, followed by a line terminator.

(Inherited from TextWriter)
WriteLine(Object)

Writes the text representation of an object to the text stream, by calling the ToString method on that object, followed by a line terminator.

(Inherited from TextWriter)
WriteLine(ReadOnlySpan<Char>)

Writes the text representation of a character span to the stream, followed by a line terminator.

WriteLine(ReadOnlySpan<Char>)

Writes the text representation of a character span to the text stream, followed by a line terminator.

(Inherited from TextWriter)
WriteLine(Single)

Writes the text representation of a 4-byte floating-point value to the text stream, followed by a line terminator.

(Inherited from TextWriter)
WriteLine(String, Object, Object, Object)

Writes out a formatted string and a new line to the stream, using the same semantics as Format(String, Object).

WriteLine(String, Object, Object, Object)

Writes out a formatted string and a new line to the text stream, using the same semantics as Format(String, Object).

(Inherited from TextWriter)
WriteLine(String, Object, Object)

Writes a formatted string and a new line to the stream, using the same semantics as the Format(String, Object, Object) method.

WriteLine(String, Object, Object)

Writes a formatted string and a new line to the text stream, using the same semantics as the Format(String, Object, Object) method.

(Inherited from TextWriter)
WriteLine(String, Object)

Writes a formatted string and a new line to the stream, using the same semantics as the Format(String, Object) method.

WriteLine(String, Object)

Writes a formatted string and a new line to the text stream, using the same semantics as the Format(String, Object) method.

(Inherited from TextWriter)
WriteLine(String, Object[])

Writes out a formatted string and a new line to the stream, using the same semantics as Format(String, Object).

WriteLine(String, Object[])

Writes out a formatted string and a new line to the text stream, using the same semantics as Format(String, Object).

(Inherited from TextWriter)
WriteLine(String, ReadOnlySpan<Object>)

Writes out a formatted string and a new line to the stream, using the same semantics as Format(String, ReadOnlySpan<Object>).

WriteLine(String, ReadOnlySpan<Object>)

Writes out a formatted string and a new line to the text stream, using the same semantics as Format(String, ReadOnlySpan<Object>).

(Inherited from TextWriter)
WriteLine(String)

Writes a string to the stream, followed by a line terminator.

WriteLine(String)

Writes a string to the text stream, followed by a line terminator.

(Inherited from TextWriter)
WriteLine(StringBuilder)

Writes the text representation of a string builder to the text stream, followed by a line terminator.

(Inherited from TextWriter)
WriteLine(UInt32)

Writes the text representation of a 4-byte unsigned integer to the text stream, followed by a line terminator.

(Inherited from TextWriter)
WriteLine(UInt64)

Writes the text representation of an 8-byte unsigned integer to the text stream, followed by a line terminator.

(Inherited from TextWriter)
WriteLineAsync()

Asynchronously writes a line terminator to the stream.

WriteLineAsync()

Asynchronously writes a line terminator to the text stream.

(Inherited from TextWriter)
WriteLineAsync(Char)

Asynchronously writes a character to the stream, followed by a line terminator.

WriteLineAsync(Char)

Asynchronously writes a character to the text stream, followed by a line terminator.

(Inherited from TextWriter)
WriteLineAsync(Char[], Int32, Int32)

Asynchronously writes a subarray of characters to the stream, followed by a line terminator.

WriteLineAsync(Char[], Int32, Int32)

Asynchronously writes a subarray of characters to the text stream, followed by a line terminator.

(Inherited from TextWriter)
WriteLineAsync(Char[])

Asynchronously writes an array of characters to the text stream, followed by a line terminator.

(Inherited from TextWriter)
WriteLineAsync(ReadOnlyMemory<Char>, CancellationToken)

Asynchronously writes the text representation of a character memory region to the stream, followed by a line terminator.

WriteLineAsync(ReadOnlyMemory<Char>, CancellationToken)

Asynchronously writes the text representation of a character memory region to the text stream, followed by a line terminator.

(Inherited from TextWriter)
WriteLineAsync(String)

Asynchronously writes a string to the stream, followed by a line terminator.

WriteLineAsync(String)

Asynchronously writes a string to the text stream, followed by a line terminator.

(Inherited from TextWriter)
WriteLineAsync(StringBuilder, CancellationToken)

Asynchronously writes the text representation of a string builder to the text stream, followed by a line terminator.

(Inherited from TextWriter)

Explicit Interface Implementations

IDisposable.Dispose()

For a description of this member, see Dispose().

(Inherited from TextWriter)

Extension Methods

ConfigureAwait(IAsyncDisposable, Boolean)

Configures how awaits on the tasks returned from an async disposable will be performed.

Applies to

See also