Device I/O StreamReader Class Constructors Fields Properties Methods Explicit Interface Implementations Remarks Applies to See also StreamWriter Class Constructors Fields Properties Methods Explicit Interface Implementations Remarks Applies to See also Examples Source/Reference
Initializes a new instance of the StreamReader class for the specified stream, with the specified character encoding and byte order mark detection option.
Initializes a new instance of the StreamReader class for the specified stream, with the specified character encoding, byte order mark detection option, and buffer size.
Initializes a new instance of the StreamReader class for the specified stream based on the specified character encoding, byte order mark detection option, and buffer size, and optionally leaves the stream open.
Initializes a new instance of the StreamReader class for the specified file name, with the specified character encoding and byte order mark detection option.
Initializes a new instance of the StreamReader class for the specified file name, with the specified character encoding, byte order mark detection option, and buffer size.
Reads a specified maximum number of characters from the current stream asynchronously and writes the data to a buffer, beginning at the specified index.
Reads a specified maximum number of characters from the current stream asynchronously and writes the data to a buffer, beginning at the specified index.
StreamReader is designed for character input in a particular encoding, whereas the Stream class is designed for byte input and output. Use StreamReader for reading lines of information from a standard text file.
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.
StreamReader defaults to UTF-8 encoding unless specified otherwise, instead of defaulting to the ANSI code page for the current system. UTF-8 handles Unicode characters correctly and provides consistent results on localized versions of the operating system. If you get the current character encoding using the CurrentEncoding property, the value is not reliable until after the first Read method, since encoding auto detection is not done until the first call to a
Read method.
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.
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.
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.
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.
Causes any buffered data to be written to the underlying stream, releases the unmanaged resources used by the StreamWriter, and optionally the managed resources.
Asynchronously writes a string to the stream, followed by a line terminator.
Explicit Interface Implementations
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.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Sample Page</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<script runat="server" >
Sub Page_Load()
Dim xstr As String
Dim xstr1 As String = "abcdefgh"
Dim xpath As String = "T:\filestream\filestream.txt"
If System.IO.Directory.Exists("T:\filestream") Then
System.IO.Directory.Delete("T:\filestream", true)
End If
System.IO.Directory.CreateDirectory("T:\filestream")
xstr = "instantiate a streamwriter of " & xpath & "<br /><br />"
Dim xswriter As New System.IO.StreamWriter(xpath,false,System.Text.Encoding.ASCII)
xstr = xstr & "Given string: " & xstr1 & "<br />"
xstr = xstr & "Write the string of streamwriter to a file of path '" & xpath & "'.<br />"
xswriter.Write(xstr1)
xswriter.Flush()
xswriter.Close()
xstr = xstr & "<br />instantiate a streamreader of " & xpath & "<br /><br />"
Dim xsreader As New System.IO.StreamReader(xpath)
xstr = xstr & "Read a line from streamreader: " & xsreader.readline() & "<br />"
lbl01.Text = xstr
End Sub
</script>
</head>
<body>
<%Response.Write("<p>Results on "& Request.ServerVariables("SERVER_SOFTWARE") & " .net: " & System.Environment.Version.ToString & " " & ScriptEngine & " Version " & ScriptEngineMajorVersion & "." & ScriptEngineMinorVersion & "</p>")%>
<% Response.Write ("<h1>This is a Sample Page of StreamReader and StreamWriter Classes</h1>") %>
<p>
<%-- Set on Page_Load --%>
<asp:Label id="lbl01" runat="server" />
</p>
</body>
</html>