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 to 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?
Streamis an abstraction for “bytes of data” that works with things other than files, like bytes sent over a network.TextReaders andTextWriters are meant for working with text.StreamReaders andStreamWriters are specific kinds which wrapStreams.The
Fileclass is specifically meant to treat a file as a unit entity, not as long stream of bytes. Hence:Stream-related classes. It usually makes no sense to keep a 10-MiBbyte[]orstringin memory, unless you really need random access to all of it.Fileclass to read and writebyte[]s orstrings.