开发者

What is the difference between calling Stream.Write and using a StreamWriter?

开发者 https://www.devze.com 2022-12-31 07:55 出处:网络
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

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 th开发者_开发技巧e 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?


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.


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);


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消