System.IO namespace
Abstract Stream Class
In the world of I/O manipulation, a stream represents a chunk of data flowing between a source and a destination.
Streams provide a common way to interact with a sequence of bytes, regardless of what kind of device (e.g., file, network connection, or printer) stores or displays the bytes in question.
The abstract System.IO.Stream
class defines several members that provide support for synchronous and asynchronous interactions with the storage medium (e.g., an underlying file or memory location).
Member | Meaning in Life |
CanRead CanWrite CanSeek | Determines whether the current stream supports reading, seeking, and/or writing. |
Close() | Closes the current stream and releases any resources (such as sockets and file handles) associated with the current stream. Kinda |
Flush() | Updates the underlying data source or repository with the current state of the buffer and then clears the buffer. |
Length | Returns the length of the stream in bytes. |
Position | Determines the position in the current stream. |
Read() ReadByte() | Reads a sequence of bytes (or a single byte) from the current stream and advances the current position in the stream by the number of bytes read. |
Seek() | Sets the position in the current stream. |
SetLength() | Sets the length of the current stream. |
Write() WriteByte( | Writes a sequence of bytes (or a single byte) to the current stream and advances the current position in this stream by the number of bytes written. |
StreamWriters and StreamReaders
The StreamWriter
and StreamReader
classes are useful whenever you need to read or write character-based data (e.g., strings). Both of these types work by default with Unicode characters; however, you can change this by supplying a properly configured System.Text.Encoding
object reference.
StreamReader
StreamReader
derives from an abstract type named TextReader
, as does the related StringReader
type. The TextReader
base class provides a limited set of functionality to each of these descendants; specifically it provides the ability to read and peek into a character stream.
StreamWriter
The StreamWriter
type (as well as StringWriter
) derives from an abstract base class named TextWriter
. This class defines members that allow derived types to write textual data to a given character stream.
StringWriters and StringReaders
You can use the StringWriter
and StringReader
types to treat textual information as a stream of in-memory characters. This can prove helpful when you would like to append character-based information to an underlying buffer.
StringWriter
and StreamWriter
both derive from the same base class (TextWriter
), so the writing logic is more or less identical. However, given the nature of StringWriter
, you can use GetStringBuilder()
method to extract a System.Text.StringBuilder
object.
Watching Files Programmatically
FileSystemWatcher
class can be quite helpful when you want to monitor (or “watch”) files on your system programmatically. Specifically, you can instruct the FileSystemWatcher
type to monitor files for any of the actions specified by the System.IO.NotifyFilters
enumeration:
You can also specify filename regex filter.
Last updated