用C#读写文件的不同方法 [英] Different ways of reading and writing files in C#

查看:91
本文介绍了用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 FileStreams, FileWriters 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆