Question real life example of FileStream/streamwriter and streamreader?

sock1992

Well-known member
Joined
May 20, 2020
Messages
107
Programming Experience
Beginner
I've recently been learning about this, and I understand they are all used to read and write files on the disk, but why and when would I need to use this? I am a beginner so apologies if it sounds real amateurish.
 
There is no point in reinventing the wheel in terms of answering this age old question when it's been answered millions of times all over the net, and best explained in the Microsoft docs :

Stream :
FileStream :
Reader :
Writer :

A filestream is a stream of data. It only deals with byte[] data, and is used for reading/writing data to files. So if you wanted to read a file or write to a file containing text, you would use a reader or writer. Whereas filestream is intended for writing data[] to files. Streamreaders and streamwriters are also used for reading and writing into network/memory streams. They are also used when reading data[] as text hence the two quotes :
Implements a TextReader that reads characters from a byte stream in a particular encoding.
Implements a TextWriter for writing characters to a stream in a particular encoding.
You will also find helpful source code examples on the pages I linked which demonstrate their usage. If you have any other specific questions, let us know.
 
I think File class is also worth mentioning, it has methods to read/write all text or all text lines of a file. It uses the StreamReader/Writer to do this, which again operates on a FileStream. If you don't need to read/write line by line or even smaller string fragments, this would be preferred.
 
Back
Top Bottom