Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference Between StreamWriter/Reader and StringWriter/Readerll

Tags:

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

like image 504
kHAzaDOOm Avatar asked Jul 12 '11 19:07

kHAzaDOOm


People also ask

What is the difference between StreamWriter and TextWriter?

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.

How does a StreamReader differ from a StreamWriter?

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.

What is StreamReader and StreamWriter?

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.


2 Answers

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

like image 197
dtb Avatar answered Oct 03 '22 16:10

dtb


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.

like image 31
SLaks Avatar answered Oct 03 '22 16:10

SLaks