What method of writing to the file to use?

I really got confused about how many different ways to write data to a file using system.IO only.
I mean, between a file stream, StreamWriter, or just system.IO.file methods ... Which one is best to use?

It even gets more confusing when you see that you can use different constructs with any of them, for example, using "use" or not.

Is there a difference between the two? Online tutorials seem to stick to just one of them and completely ignore the others. Some of these manuals even use different methods of referencing a file for writing (using file types in some cases, FileInfo types in others, or even just a string for their path / name).

Is any of them more effective than the other?

+4
source share
5 answers

Stream is an abstraction for β€œdata bytes” that works with things other than files, such as bytes sent over a network.

TextReader and TextWriter are designed to work with text . StreamReader and StreamWriter are the specific types that complete Stream s.

The File class is designed specifically to treat a file as a unit object, and not as a long stream of bytes. Hence:

  • If the file can be large (I would say that it means 1 MiB +), use the Stream classes. Usually it makes no sense to store a 10 megabyte byte[] or string in memory, unless you really need random access to everyone .
  • If it is always small (so it makes sense to store everything in memory), you can simply use the File class to read and write byte[] or string s.
+6
source

There are so many ways, because there are so many ways to structure the data to be sent. Do you have string arrays? Are you transferring data from some other source (e.g. network stream)? Do you want to write lines of text such as a magazine?

This will help you find out what you want to write, then we can help you decide how to do it.

Oh, and always use "use" if you can. You get a cleanup of resources, even if your code does not fit, which is good.

+3
source

The StreamWriter and StreamReader classes are for reading and writing text, and the FileStream class is for binary (non-text) data.

+2
source

There are other interesting points on this subject:

FileStream vs / difference StreamWriter?

+1
source

It depends on what level of abstraction you need to work with and what data you work with (text versus binary code).

If you just need to upload the data to a file, you can use the helper methods for File class- WriteAllBytes() for binary (it just writes for FileStream for you) and WriteAllLines() or WriteAllText() for text (using StreamWriter using UTF8NoBOM encoding, unless otherwise specified).

A StreamWriter allows you to 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 to use a low-level control, for example, specify the file mode, system rights, file sharing and other such parameters, you need to work with the file stream. You can also specify these parameters for working with text, passing it to StreamWriter or treating the text as bytes (for example, using the Encoding object).

+1
source

All Articles