用C#读写文件的不同方法 [英] Different ways of reading and writing files in C#
问题描述
我正在尝试理解读写文件的各种方法,以及它们的优缺点.就像何时在File.Create或StreamReader/StreamWriter时使用TextWriter/TextReader FileStream等.
I am trying to understand different ways of reading and writing files with their advantages and disadvantages. Like when to use TextWriter/TextReader when File.Create or StreamReader/StreamWriter FileStream etc.
什么时候使用什么?
推荐答案
File.*
静态方法只是构造新的FileStream
s,FileWriter
s等的简单方法.它们非常有用-我通常使用File.*优先于显式调用构造函数,除非我需要一些不需要的行为.
The File.*
static methods are just simple ways of constructing new FileStream
s, FileWriter
s etc. They're very useful - I generally use File.* in preference to explicitly calling the constructors unless I need some behaviour which isn't catered for.
关键的关键点是:
- 对于二进制数据,请使用
Stream
- 对于文本数据,请使用
TextWriter
/TextReader
- For binary data, use
Stream
- For text data, use
TextWriter
/TextReader
如果您开始尝试使用TextReader
读取二进制数据,则会发生坏事.
If you start trying to read binary data with TextReader
, bad things will happen.
这篇关于用C#读写文件的不同方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!