简体   繁体   English

使用StreamWriter写入MemoryStream返回空

[英]Writing to MemoryStream with StreamWriter returns empty

I am not sure what I am doing wrong, have seen a lot of examples, but can't seem to get this working. 我不确定自己在做什么错,已经看了很多例子,但似乎无法解决这个问题。

public static Stream Foo()
{
    var memStream = new MemoryStream();
    var streamWriter = new StreamWriter(memStream);

    for (int i = 0; i < 6; i++)
        streamWriter.WriteLine("TEST");

    memStream.Seek(0, SeekOrigin.Begin);
    return memStream;
}

I am doing a simple test on this method to try and get it to pass, but no matter what, my collection count is 0. 我正在对此方法进行简单的测试,以尝试使其通过,但是无论如何,我的收藏计数为0。

[Test]
public void TestStreamRowCount()
{
    var stream = Foo();

    using (var reader = new StreamReader(stream))
    {
        var collection = new List<string>();
        string input;

        while ((input = reader.ReadLine()) != null)
            collection.Add(input);

        Assert.AreEqual(6, collection.Count);
    }
}

Note: I changed some syntax above without compiling in the Test method. 注意:我在不编译Test方法的情况下更改了上面的某些语法。 What is more important is the first method which seems to be returning an empty stream (my reader.ReadLine() always reads once). 更重要的是第一个方法似乎返回一个空流(我的reader.ReadLine()总是读取一次)。 Not sure what I am doing wrong. 不知道我在做什么错。 Thank you. 谢谢。

You are forgetting to flush your StreamWriter instance. 您忘记了刷新StreamWriter实例。

public static Stream Foo()
{
    var memStream = new MemoryStream();
    var streamWriter = new StreamWriter(memStream);

    for (int i = 0; i < 6; i++)
        streamWriter.WriteLine("TEST");

    streamWriter.Flush();                                   <-- need this
    memStream.Seek(0, SeekOrigin.Begin);
    return memStream;
}

Also note that StreamWriter is supposed to be disposed of, since it implements IDisposable , but that in turn generates another problem, it will close the underlying MemoryStream as well. 还要注意,应该StreamWriter ,因为它实现了IDisposable ,但继而又产生了另一个问题,它将也关闭底层的MemoryStream

Are you sure you want to return a MemoryStream here? 您确定要在此处返回MemoryStream吗?

I would change the code to this: 我将代码更改为此:

public static byte[] Foo()
{
    using (var memStream = new MemoryStream())
    using (var streamWriter = new StreamWriter(memStream))
    {
        for (int i = 0; i < 6; i++)
            streamWriter.WriteLine("TEST");

        streamWriter.Flush();
        return memStream.ToArray();
    }
}

[Test]
public void TestStreamRowCount()
{
    var bytes = Foo();

    using (var stream = new MemoryStream(bytes))
    using (var reader = new StreamReader(stream))
    {
        var collection = new List<string>();
        string input;

        while ((input = reader.ReadLine()) != null)
            collection.Add(input);

        Assert.AreEqual(6, collection.Count);
    }
}

Since you are not using "using" or streamWriter.Flush() the writer did not commit changes to the stream. 由于您未使用“ using”或streamWriter.Flush(),因此编写器未将更改提交到流。 As result Stream itslef does not have data yet. 结果,Stream itslef还没有数据。 In general you want to wrap manipulation with Stream and StremaWriter instances with using. 通常,您要使用using包装带有Stream和StremaWriter实例的操作。

You also should consider returning new instance of MemoryStream: 您还应该考虑返回MemoryStream的新实例:

using(var memStream = new MemoryStream())
{
   ....
   return new MemoryStream(memStream.ToArray(), false /*writable*/);
}

编写行后,尝试刷新streamWriter。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM