FileStream vs/differences StreamWriter?
Solution 1
What is different between FileStream and StreamWriter in dotnet?
A FileStream
is a Stream
. Like all Streams it only deals with byte[]
data.
A StreamWriter : TextWriter
is a Stream-decorator. A TextWriter encodes the primitive type like string, int and char to byte[]
and then writes hat to the linked Stream.
What context are you supposed to use it? What is their advantage and disadvantage?
You use a bare FileStream when you have byte[]
data. You add a StreamWriter
when you want to write text. Use a Formatter or a Serializer to write more complex data.
Is it possible to combine these two into one?
Yes. You always need a Stream to create a StreamWriter. The helper method System.IO.File.CreateText("path")
will create them in combination and then you only have to Dispose() the outer writer.
Solution 2
FileStream writes bytes, StreamWriter writes text. That's all.
Solution 3
A FileStream is explicitly intended for working files.
A StreamWriter can be used to stream to any type of Stream - network sockets, files, etc.
ScottGu explains the different Stream objects quite nicely here: http://www.codeguru.com/Csharp/Csharp/cs_data/streaming/article.php/c4223
Solution 4
They are two different levels used in outputting information to known data sources.
A FileStream
is a type of Stream, which is conceptually a mechanism that points to some location and can handle incoming and/or outgoing data to and from that location. Streams exist for reading/writing to files, network connections, memory, pipes, the console, debug and trace listeners, and a few other types of data sources. Specifically, a FileStream
exists to perform reads and writes to the file system. Most streams are pretty low-level in their usage, and deal with data as bytes.
A StreamWriter
is a wrapper for a Stream that simplifies using that stream to output plain text. It exposes methods that take strings instead of bytes, and performs the necessary conversions to and from byte arrays. There are other Writers; the other main one you'd use is the XmlTextWriter
, which facilitates writing data in XML format. There are also Reader counterparts to the Writers that similarly wrap a Stream and facilitate getting the data back out.
Solution 5
Well, from the MSDN for FileStream
:
Exposes a Stream around a file, supporting both synchronous and asynchronous read and write operations.
and the MSDN for StreamWriter
:
Implements a TextWriter for writing characters to a stream in a particular encoding.
The most obvious difference is that FileStream
allows read/write operations, while StreamWriter
is write only.
The StreamWriter
page goes on to add:
StreamWriter is designed for character output in a particular encoding, whereas classes derived from Stream are designed for byte input and output.
So a second difference is that FileStream
is for bytes, while StreamWriter
is for text.
HelloWorld1
Updated on July 08, 2022Comments
-
HelloWorld1 almost 2 years
Question:
What is different between
FileStream
andStreamWriter
in .Net?What context are you supposed to use it? What is their advantage and disadvantage?
Is it possible to combine these two into one?
-
David over 13 yearsActually, that's not "all"... But it is one correct difference.
-
ThunderGr over 10 yearsExcellent reference for streams, although it does not cover random access R/W streams. Especially the serialization/deserialization information and the demonstration of non-file streams are very useful. +1
-
John Smith about 7 yearsAlso is important to note that
FileStream
is a type of stream, that is specifically tailored towards files. Streams natively work with bytes, howeverStreamWriter
/ Reader will write / read text on any stream, not justFileStream
s. For example,MemoryStreams
,NetworkStreams
, etc..