调用 Stream.Write 和使用 StreamWriter 有什么区别?

发布于 2024-09-02 08:52:05 字数 1053 浏览 5 评论 0原文

实例化 Stream 对象(例如 MemoryStream)和调用 memoryStream.Write() 方法写入流有什么区别,以及使用流实例化 StreamWriter 对象并调用 streamWriter.Write()

考虑以下场景:

您有一个方法,它接受 Stream、写入值并返回它。该流是稍后读取的,因此必须重置位置。有两种可能的方法(似乎都有效)。

// Instantiate a MemoryStream somewhere
//     - Passed to the following two methods
MemoryStream memoryStream = new MemoryStream();

// Not using a StreamWriter
private static Stream WriteToStream(Stream stream, string value)
{
    stream.Write(Encoding.Default.GetBytes(value), 0, value.Length);
    stream.Flush();
    stream.Position = 0;
    return stream;
}

// Using a StreamWriter
private static Stream WriteToStreamWithWriter(Stream stream, string value)
{
    StreamWriter sw = new StreamWriter(stream);
    sw.Write(value, 0, value.Length);
    sw.Flush();
    stream.Position = 0;
    return stream;
}

这部分是一个范围问题,因为我不想在写入流后关闭流,因为稍后会读取它。我当然也不想处置它,因为这会关闭我的流。区别似乎在于,不使用 StreamWriter 会引入对 Encoding.Default 的直接依赖,但我不确定这有什么大不了的。如果有的话,有什么区别?

What is the difference between instantiating a Stream object, such as MemoryStream and calling the memoryStream.Write() method to write to the stream, and instantiating a StreamWriter object with the stream and calling streamWriter.Write()?

Consider the following scenario:

You have a method that takes a Stream, writes a value, and returns it. The stream is read from later on, so the position must be reset. There are two possible ways of doing it (both seem to work).

// Instantiate a MemoryStream somewhere
//     - Passed to the following two methods
MemoryStream memoryStream = new MemoryStream();

// Not using a StreamWriter
private static Stream WriteToStream(Stream stream, string value)
{
    stream.Write(Encoding.Default.GetBytes(value), 0, value.Length);
    stream.Flush();
    stream.Position = 0;
    return stream;
}

// Using a StreamWriter
private static Stream WriteToStreamWithWriter(Stream stream, string value)
{
    StreamWriter sw = new StreamWriter(stream);
    sw.Write(value, 0, value.Length);
    sw.Flush();
    stream.Position = 0;
    return stream;
}

This is partially a scope problem, as I don't want to close the stream after writing to it since it will be read from later. I also certainly don't want to dispose it either, because that will close my stream. The difference seems to be that not using a StreamWriter introduces a direct dependency on Encoding.Default, but I'm not sure that's a very big deal. What's the difference, if any?

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

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

发布评论

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

评论(4

深海不蓝 2024-09-09 08:52:05

使用 StreamWriter,您可以拥有更高级别的重载,可以将各种类型写入流,而无需担心细节。例如,您的代码

sw.Write(value, 0, value.Length);

实际上可能只是

sw.Write(value);

使用 StreamWriter.Write(string) 重载。

With the StreamWriter you have higher level overloads that can write various types to the stream without you worrying about the details. For example your code

sw.Write(value, 0, value.Length);

Could actually just be

sw.Write(value);

Using the StreamWriter.Write(string) overload.

鸠魁 2024-09-09 08:52:05

一个区别是 new StreamWriter(stream) 默认使用 UTF-8 编码,因此它将支持 Unicode 数据。 Encoding.Default(至少在我的机器上)是固定大小的代码页(例如 Windows-1250),并且仅支持 ASCII 和有限的国家字符集(总共 256 个不同的字符)。

您确实不应该执行以下操作:

stream.Write(encoding.GetBytes(value), 0, value.Length);

您使用的编码具有 1 字节的固定大小,这只是一个巧合。 (它不适用于 UTF-16、UTF-8 和非 ASCII 数据。)相反,如果您需要直接写入流,请执行以下操作:

byte[] byteData=encoding.GetBytes(value);
stream.Write(byteData, 0, byteData.Length);

One difference is that new StreamWriter(stream) by default uses UTF-8 encoding, so it will support Unicode data. Encoding.Default (at least on my machine) is a fixed-size code page (such as Windows-1250) and only supports ASCII and a limited set of national characters (256 different characters in total).

You really shouldn't do the following:

stream.Write(encoding.GetBytes(value), 0, value.Length);

It's just a coincidence that the encoding you use has a fixed size of 1 byte. (It wouldn't work with UTF-16, or with UTF-8 and non-ASCII data.) Instead, if you need to directly write to a stream, do:

byte[] byteData=encoding.GetBytes(value);
stream.Write(byteData, 0, byteData.Length);
倾城月光淡如水﹏ 2024-09-09 08:52:05

StreamWriter 是 Stream 的超类,它实现了 TextWriter 以更轻松地处理文本。但由于它是一个超类,因此除了文本处理方法之外,它还具有所有相同的方法。这就是为什么您在第一个示例中需要 Encoding.Default.GetBytes(value) 而在第二个示例中不需要。

StreamWriter is a superclass of Stream that implements a TextWriter for easier handling of text. But since is a super class it has all the same methods in addition to the text handling ones. This why you need the Encoding.Default.GetBytes(value) in the first example and in the second you do not.

时光匆匆的小流年 2024-09-09 08:52:05

byte[] 数组而言,没什么,StreamWriter 确实引入了其他更有用的方法,尽管可以处理其他类型。

In terms of byte[] arrays, nothing, StreamWriter does introduce other more useful methods though for working with other types.

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