开发者

Which writing to file method to use?

开发者 https://www.devze.com 2023-03-18 03:00 出处:网络
I\'m really getting confused by how many different ways there is to write data to a file just with System.IO.

I'm really getting confused by how many different ways there is to write data to a file just with System.IO.

I mean, between FileStream, 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.

TextReaders and TextWriters are meant for working with text. StreamReaders and StreamWriters are specific kinds which wrap Streams.

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-MiB byte[] or string 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 write byte[]s or strings.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号