Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 992
John Nelson Avatar asked May 21 '10 12:05

John Nelson


People also ask

What is a StreamWriter?

The StreamWriter class in C# is used for writing characters to a stream. It uses the TextWriter class as a base class and provides the overload methods for writing data into a file. The StreamWriter is mainly used for writing multiple characters of data into a file.

What is the difference between FileStream and StreamWriter?

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.

What is use of flush call on StreamWriter in C#?

Clears all buffers for the current writer and causes any buffered data to be written to the underlying stream.

What is a stream and types of streams in C#?

The stream is basically the sequence of bytes passing through the communication path. There are two main streams: the input stream and the output stream. The input stream is used for reading data from file (read operation) and the output stream is used for writing into the file (write operation).


1 Answers

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.

like image 100
Chris Taylor Avatar answered Oct 08 '22 22:10

Chris Taylor