FileStream 与/差异 StreamWriter? [英] FileStream vs/differences StreamWriter?
问题描述
问题:
.Net中的FileStream
和StreamWriter
有什么不同?
你应该在什么上下文中使用它?他们的优势和劣势是什么?
What context are you supposed to use it? What is their advantage and disadvantage?
是否可以将这两者合二为一?
Is it possible to combine these two into one?
推荐答案
dotnet 中的 FileStream 和 StreamWriter 有什么不同?
What is different between FileStream and StreamWriter in dotnet?
一个 FileStream
是一个 Stream
.像所有 Streams 一样,它只处理 byte[]
数据.
A FileStream
is a Stream
. Like all Streams it only deals with byte[]
data.
A StreamWriter : TextWriter
,是一个流装饰器.TextWriter 将字符串或字符等文本数据编码为 byte[]
,然后将其写入链接的 Stream
.
A StreamWriter : TextWriter
, is a Stream-decorator. A TextWriter encodes Text data like string or char to byte[]
and then writes it to the linked Stream
.
你应该在什么上下文中使用它?他们的优势和劣势是什么?
What context are you supposed to use it? What is their advantage and disadvantage?
当您有 byte[]
数据时,您使用裸 FileStream.当你想写文本时,你添加一个 StreamWriter
.使用格式化程序或序列化程序写入更复杂的数据.
You use a bare FileStream when you have byte[]
data. You add a StreamWriter
when you want to write text. Use a Formatter or a Serializer to write more complex data.
是否可以将这两者合二为一?
Is it possible to combine these two into one?
是的.你总是需要一个 Stream 来创建一个 StreamWriter.辅助方法 System.IO.File.CreateText("path")
将组合创建它们,然后您只需要 Dispose() 外部编写器.
Yes. You always need a Stream to create a StreamWriter. The helper method System.IO.File.CreateText("path")
will create them in combination and then you only have to Dispose() the outer writer.
这篇关于FileStream 与/差异 StreamWriter?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!