Im very confused with the exact difference between them and different usage approach of these two TextWriter/Reader derived types StringWriter/Reader and StreamReader/Reader. I know that using them we can deal easily with character based data in stream avoiding byte fuss as working direclty using Filestream...
TextWriter is an abstract base class for writing text characters to a destination. StreamWriter writes text characters (converted to bytes) to a stream of bytes.
A StreamReader is used whenever data is required to be read from a file. A Streamwriter is used whenever data needs to be written to a file.
The StreamReader and StreamWriter classes are used for reading from and writing data to text files. These classes inherit from the abstract base class Stream, which supports reading and writing bytes into a file stream.
TextWriter/Reader is an abstract class. It provides an abstraction for writing/reading character based data to/from a data source.
StreamWriter/Reader is a concrete implementation that uses a writeable/readable Stream as data source. Since a Stream is abstraction for writing/reading byte based data, an Encoding instance is required for the translation between characters and bytes.
StringWriter/Reader is a concrete implementation that uses a StringBuilder/string as data source.
The Stream*
classes read from a Stream
.
The String*
classes read from a String
(and write to a StringBuilder
).
You can write a method that takes a TextReader
, and call it with a StreamReader
or a StringReader
for it to read from a stream or a string.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With