FileStream 与 StreamWriter 的区别?

发布于 2024-10-17 01:27:17 字数 136 浏览 7 评论 0原文

.NET 中的 FileStreamStreamWriter 有什么区别?

你应该在什么背景下使用它?他们的优点和缺点是什么?

是否有可能将这两者合而为一?

What is difference between FileStream and StreamWriter in .NET?

What context are you supposed to use it? What is their advantage and disadvantage?

Is it possible to combine these two into one?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

南风起 2024-10-24 01:27:17

dotnet 中的 FileStream 和 StreamWriter 有什么不同?

FileStream 是一个Stream。与所有 Stream 一样,它只处理 byte[] 数据。

StreamWriter : TextWriter 是一个流装饰器。 TextWriter 将 string、int 和 char 等基本类型编码为 byte[],然后将其写入链接的 Stream。

您应该在什么上下文中使用它?他们的优点和缺点是什么?

当您有 byte[] 数据时,您可以使用裸 FileStream。当您想要写入文本时,您可以添加一个StreamWriter。使用格式化程序或序列化程序写入更复杂的数据。

是否可以将这两者合而为一?

是的。您始终需要一个 Stream 来创建 StreamWriter。辅助方法 System.IO.File.CreateText("path") 将组合创建它们,然后您只需 Dispose() 外部编写器即可。

What is different between FileStream and StreamWriter in dotnet?

A FileStream is a Stream. Like all Streams it only deals with byte[] data.

A StreamWriter : TextWriter is a Stream-decorator. A TextWriter encodes the primitive types like string, int and char to byte[] and then writes that to the linked Stream.

What context are you supposed to use it? What is their advantage and disadvantage?

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?

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.

许久 2024-10-24 01:27:17

FileStream 写入字节,StreamWriter 写入文本。就这样。

FileStream writes bytes, StreamWriter writes text. That's all.

云仙小弟 2024-10-24 01:27:17

FileStream 明确用于工作文件。

StreamWriter 可用于流式传输到任何类型的 Stream - 网络套接字, ScottGu 在这里很好地解释了不同的

Stream 对象:http://www. codeguru.com/Csharp/Csharp/cs_data/streaming/article.php/c4223

A FileStream is explicitly intended for working files.

A StreamWriter can be used to stream to any type of Stream - network sockets, files, etc.

ScottGu explains the different Stream objects quite nicely here: http://www.codeguru.com/Csharp/Csharp/cs_data/streaming/article.php/c4223

鸢与 2024-10-24 01:27:17

它们是用于将信息输出到已知数据源的两个不同级别。

FileStream 是 Stream 的一种类型,从概念上讲,它是一种指向某个位置并可以处理传入和/或传出该位置的数据的机制。流用于读取/写入文件、网络连接、内存、管道、控制台、调试和跟踪侦听器以及一些其他类型的数据源。具体来说,FileStream 的存在是为了执行对文件系统的读取和写入。大多数流的使用级别都相当低,并且将数据作为字节处理。

StreamWriter 是 Stream 的包装器,它简化了使用该流输出纯文本的过程。它公开了采用字符串而不是字节的方法,并执行与字节数组之间的必要转换。还有其他作家;您要使用的另一个主要工具是 XmlTextWriter,它有助于以 XML 格式写入数据。还有与写入器相对应的读取器,它们类似地包装流并方便取回数据。

They are two different levels used in outputting information to known data sources.

A FileStream is a type of Stream, which is conceptually a mechanism that points to some location and can handle incoming and/or outgoing data to and from that location. Streams exist for reading/writing to files, network connections, memory, pipes, the console, debug and trace listeners, and a few other types of data sources. Specifically, a FileStream exists to perform reads and writes to the file system. Most streams are pretty low-level in their usage, and deal with data as bytes.

A StreamWriter is a wrapper for a Stream that simplifies using that stream to output plain text. It exposes methods that take strings instead of bytes, and performs the necessary conversions to and from byte arrays. There are other Writers; the other main one you'd use is the XmlTextWriter, which facilitates writing data in XML format. There are also Reader counterparts to the Writers that similarly wrap a Stream and facilitate getting the data back out.

饮湿 2024-10-24 01:27:17

好吧,来自 MSDN 的 FileStream< /a>:

围绕文件公开 Stream,支持同步和异步读写操作。

以及 StreamWriter

实现 TextWriter,用于以特定编码将字符写入流。

最明显的区别是 FileStream 允许读/写操作,而 StreamWriter 只能写。

StreamWriter 页面继续添加:

StreamWriter 专为特定编码中的字符输出而设计,而从 Stream 派生的类则专为字节输入和输出而设计。

第二个区别是 FileStream 用于字节,而 StreamWriter 用于文本。

Well, from the MSDN for FileStream:

Exposes a Stream around a file, supporting both synchronous and asynchronous read and write operations.

and the MSDN for StreamWriter:

Implements a TextWriter for writing characters to a stream in a particular encoding.

The most obvious difference is that FileStream allows read/write operations, while StreamWriter is write only.

The StreamWriter page goes on to add:

StreamWriter is designed for character output in a particular encoding, whereas classes derived from Stream are designed for byte input and output.

So a second difference is that FileStream is for bytes, while StreamWriter is for text.

寄离 2024-10-24 01:27:17

一个关键的区别(除了上述注释之外)可能是 FileStream 支持随机磁盘访问读取和写入任何指定的 FileStream.Position。对于大型文件修改,这可能是无价的。

One key difference (in addition to the above comments), could be that FileStream supports random disk access read and writes to any specified FileStream.Position. For large file modifications, that can be invaluable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文