Sideway
output.to from Sideway
Draft for Information Only

Content

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

Device I/O

The main components used in .NET framework to access the device i/o are Stream, BufferedStream, FileStream, MemoryStream, StreamReader, StreamWriter, StringReader, StringWriter, TextReader, TextWriter, BinaryReader, and BinaryWriter.

StreamReader Class

Implements a TextReader that reads characters from a byte stream in a particular encoding.

NamespaceSystem.IO AssembliesSystem.IO.dll, mscorlib.dll, netstandard.dll, System.Runtime.Extensions.dll
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public class StreamReader : System.IO.TextReader

Inheritance: Object->MarshalByRefObject->TextReader->StreamReader

Attributes: ComVisibleAttribute, SerializableAttribute

Constructors

StreamReader(Stream)

Initializes a new instance of the StreamReader class for the specified stream.

StreamReader(Stream, Boolean)

Initializes a new instance of the StreamReader class for the specified stream, with the specified byte order mark detection option.

StreamReader(Stream, Encoding)

Initializes a new instance of the StreamReader class for the specified stream, with the specified character encoding.

StreamReader(Stream, Encoding, Boolean)

Initializes a new instance of the StreamReader class for the specified stream, with the specified character encoding and byte order mark detection option.

StreamReader(Stream, Encoding, Boolean, Int32)

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.

StreamReader(Stream, Encoding, Boolean, Int32, Boolean)

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.

StreamReader(String)

Initializes a new instance of the StreamReader class for the specified file name.

StreamReader(String, Boolean)

Initializes a new instance of the StreamReader class for the specified file name, with the specified byte order mark detection option.

StreamReader(String, Encoding)

Initializes a new instance of the StreamReader class for the specified file name, with the specified character encoding.

StreamReader(String, Encoding, Boolean)

Initializes a new instance of the StreamReader class for the specified file name, with the specified character encoding and byte order mark detection option.

StreamReader(String, Encoding, Boolean, Int32)

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.

Fields

Null

A StreamReader object around an empty stream.

Properties

BaseStream

Returns the underlying stream.

CurrentEncoding

Gets the current character encoding that the current StreamReader object is using.

EndOfStream

Gets a value that indicates whether the current stream position is at the end of the stream.

Methods

Close()

Closes the StreamReader object and the underlying stream, and releases any system resources associated with the reader.

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)
DiscardBufferedData()

Clears the internal buffer.

Dispose()

Releases all resources used by the TextReader object.

(Inherited from TextReader)
Dispose(Boolean)

Closes the underlying stream, releases the unmanaged resources used by the StreamReader, and optionally releases the managed resources.

Equals(Object)

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

(Inherited from Object)
GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetLifetimeService()

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()

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)
Peek()

Returns the next available character but does not consume it.

Read()

Reads the next character from the input stream and advances the character position by one character.

Read(Char[], Int32, Int32)

Reads a specified maximum of characters from the current stream into a buffer, beginning at the specified index.

ReadAsync(Char[], Int32, Int32)

Reads a specified maximum number of characters from the current stream asynchronously and writes the data to a buffer, beginning at the specified index.

ReadBlock(Char[], Int32, Int32)

Reads a specified maximum number of characters from the current stream and writes the data to a buffer, beginning at the specified index.

ReadBlockAsync(Char[], Int32, Int32)

Reads a specified maximum number of characters from the current stream asynchronously and writes the data to a buffer, beginning at the specified index.

ReadLine()

Reads a line of characters from the current stream and returns the data as a string.

ReadLineAsync()

Reads a line of characters asynchronously from the current stream and returns the data as a string.

ReadToEnd()

Reads all characters from the current position to the end of the stream.

ReadToEndAsync()

Reads all characters from the current position to the end of the stream asynchronously and returns them as one string.

ToString()

Returns a string that represents the current object.

(Inherited from Object)

Explicit Interface Implementations

Remarks

  • 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.

    By default, a StreamReader is not thread safe. See TextReader.Synchronized for a thread-safe wrapper.

    The Read(Char[], Int32, Int32) and Write(Char[], Int32, Int32) method overloads read and write the number of characters specified by the count parameter. These are to be distinguished from BufferedStream.Read and BufferedStream.Write, which read and write the number of bytes specified by the count parameter. Use the BufferedStream methods only for reading and writing an integral number of byte array elements.

    Note

    When reading from a Stream, it is more efficient to use a buffer that is the same size as the internal buffer of the stream.

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

Applies to

.NET Framework: 4.8, 4.7.2, 4.7.1, 4.7, 4.6.2, 4.6.1, 4.6, 4.5.2, 4.5.1, 4.5, 4.0, 3.5, 3.0, 2.0, 1.1

See also

StreamWriter Class

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

NamespaceSystem.IO AssembliesSystem.IO.dll, mscorlib.dll, netstandard.dll, System.Runtime.Extensions.dll
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public class StreamWriter : System.IO.TextWriter

Inheritance: Object->MarshalByRefObject->TextWriter->StreamWriter

Attributes: >ComVisibleAttribute, >SerializableAttribute

Constructors

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(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, 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, 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(String)

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

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, 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, 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.

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.

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.

Equals(Object)

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

(Inherited from Object)
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.

GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetLifetimeService()

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()

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[])

Writes a character array to the stream.

Write(Char[], Int32, Int32)

Writes a subarray of characters 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(Single)

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

(Inherited from TextWriter)
Write(String)

Writes a string to the stream.

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 array to the text stream asynchronously.

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

Asynchronously writes a subarray of characters to the stream.

WriteAsync(String)

Asynchronously writes a string to the stream.

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[])

Writes an array of characters 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(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(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(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(Char)

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

WriteLineAsync(Char[])

Asynchronously writes an array of characters 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(String)

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.

    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.

Applies to

.NET Framework: 4.8, 4.7.2, 4.7.1, 4.7, 4.6.2, 4.6.1, 4.6, 4.5.2, 4.5.1, 4.5, 4.0, 3.5, 3.0, 2.0, 1.1

See also

Examples

Examples of StreamReader and StreamWriter Classes
ASP.NET Code Input:
<!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>
HTML Web Page Embedded Output:

Source/Reference


©sideway

ID: 200700012 Last Updated: 12/7/2020 Revision: 0 Ref:

close

References

  1. Active Server Pages,  , http://msdn.microsoft.com/en-us/library/aa286483.aspx
  2. ASP Overview,  , http://msdn.microsoft.com/en-us/library/ms524929%28v=vs.90%29.aspx
  3. ASP Best Practices,  , http://technet.microsoft.com/en-us/library/cc939157.aspx
  4. ASP Built-in Objects,  , http://msdn.microsoft.com/en-us/library/ie/ms524716(v=vs.90).aspx
  5. Response Object,  , http://msdn.microsoft.com/en-us/library/ms525405(v=vs.90).aspx
  6. Request Object,  , http://msdn.microsoft.com/en-us/library/ms524948(v=vs.90).aspx
  7. Server Object (IIS),  , http://msdn.microsoft.com/en-us/library/ms525541(v=vs.90).aspx
  8. Application Object (IIS),  , http://msdn.microsoft.com/en-us/library/ms525360(v=vs.90).aspx
  9. Session Object (IIS),  , http://msdn.microsoft.com/en-us/library/ms524319(8v=vs.90).aspx
  10. ASPError Object,  , http://msdn.microsoft.com/en-us/library/ms524942(v=vs.90).aspx
  11. ObjectContext Object (IIS),  , http://msdn.microsoft.com/en-us/library/ms525667(v=vs.90).aspx
  12. Debugging Global.asa Files,  , http://msdn.microsoft.com/en-us/library/aa291249(v=vs.71).aspx
  13. How to: Debug Global.asa files,  , http://msdn.microsoft.com/en-us/library/ms241868(v=vs.80).aspx
  14. Calling COM Components from ASP Pages,  , http://msdn.microsoft.com/en-us/library/ms524620(v=VS.90).aspx
  15. IIS ASP Scripting Reference,  , http://msdn.microsoft.com/en-us/library/ms524664(v=vs.90).aspx
  16. ASP Keywords,  , http://msdn.microsoft.com/en-us/library/ms524672(v=vs.90).aspx
  17. Creating Simple ASP Pages,  , http://msdn.microsoft.com/en-us/library/ms524741(v=vs.90).aspx
  18. Including Files in ASP Applications,  , http://msdn.microsoft.com/en-us/library/ms524876(v=vs.90).aspx
  19. ASP Overview,  , http://msdn.microsoft.com/en-us/library/ms524929(v=vs.90).aspx
  20. FileSystemObject Object,  , http://msdn.microsoft.com/en-us/library/z9ty6h50(v=vs.84).aspx
  21. http://msdn.microsoft.com/en-us/library/windows/desktop/ms675944(v=vs.85).aspx,  , ADO Object Model
  22. ADO Fundamentals,  , http://msdn.microsoft.com/en-us/library/windows/desktop/ms680928(v=vs.85).aspx
close
IMAGE

Home 5

Business

Management

HBR 3

Information

Recreation

Hobbies 8

Culture

Chinese 1097

English 337

Reference 67

Computer

Hardware 149

Software

Application 198

Digitization 119

Numeric 19

Programming

Web 283

Unicode 494

HTML 65

CSS 58

ASP.NET 97

OS 389

DeskTop 7

Python 19

Knowledge

Mathematics

Formulas 8

Algebra 25

Number Theory 206

Trigonometry 18

Geometry 18

Calculus 67

Complex Analysis 21

Engineering

Tables 8

Mechanical

Mechanics 1

Rigid Bodies

Statics 92

Dynamics 37

Fluid 5

Fluid Kinematics 5

Control

Process Control 1

Acoustics 19

FiniteElement 2

Physics

Electric 27

Biology 1

Geography 1


Copyright © 2000-2020 Sideway . All rights reserved Disclaimers last modified on 06 September 2019