Which writing to file method to use?
I'm really getting confused by how many different ways there is to write data to a file just with System.IO.
I mean, betweenFileStream
, StreamWriter
or just the System.IO.file methods... Which one is the best t开发者_开发问答o use?
It even gets more confusing when you see you can use different constructs with any of them, like using using
or not.
Is there any difference between them? Online tutorials seems to only stick to one of them and completely ignores the other ones. Some of these tutorials are even using different ways of referencing the file to write in (using the File
type in some cases, FileInfo
types in others, or even just a string for its path/name).
Any of them is more efficient than the other?
Stream
is an abstraction for "bytes of data" that works with things other than files, like bytes sent over a network.
TextReader
s and TextWriter
s are meant for working with text. StreamReader
s and StreamWriter
s are specific kinds which wrap Stream
s.
The File
class is specifically meant to treat a file as a unit entity, not as long stream of bytes. Hence:
- If the file could be big (I'd say that means 1 MiB+), use the
Stream
-related classes. It usually makes no sense to keep a 10-MiBbyte[]
orstring
in memory, unless you really need random access to all of it. - If it's always small (so that it makes sense to keep it all in memory), you can just use the
File
class to read and writebyte[]
s orstring
s.
There are so many ways because there are many ways to structure data to send. Do you have arrays of strings? Are you streaming data from some other source (like a network stream)? Do you want to write lines of text like a log?
It would help to know what you want to write, then we can help you decide how.
Oh, and always use 'using' if you can. You get resource cleanup even if your code fails which is a good thing.
The StreamWriter
and StreamReader
classes are designed for reading and writing text, and the FileStream
class is designed for binary (non-text) data.
There are some other interesting points about this on the following question:
FileStream vs/differences StreamWriter?
It depends on which level of abstraction you need to work with and what kind of data you're working with (text vs. binary).
If you just need to dump data into a file, you can use the helper methods on the File
class—WriteAllBytes()
for binary (it just writes to a FileStream
for you) and the WriteAllLines()
or WriteAllText()
for text (they use a StreamWriter
for this, using an UTF8NoBOM
encoding unless otherwise specified).
A StreamWriter
lets you write text to a specified stream, which may be a FileStream
or some other kind of stream.
If you need to write bytes and you want low-level control, such as specifying file mode, system rights, file-sharing, and other such options, you need to work with a file stream. You may also specify these options to work with text, by passing it to a StreamWriter
or treating the text as bytes (e.g., by using an Encoding
object).
精彩评论