将字符串写入流并将其读回不起作用

Writing String to Stream and reading it back does not work(将字符串写入流并将其读回不起作用)

本文介绍了将字符串写入流并将其读回不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一个字符串写入一个流(在这种情况下是一个 MemoryStream)并一个一个地读取字节.

I want to write a String to a Stream (a MemoryStream in this case) and read the bytes one by one.

stringAsStream = new MemoryStream();
UnicodeEncoding uniEncoding = new UnicodeEncoding();
String message = "Message";

stringAsStream.Write(uniEncoding.GetBytes(message), 0, message.Length);

Console.WriteLine("This:		" + (char)uniEncoding.GetBytes(message)[0]);
Console.WriteLine("Differs from:	" + (char)stringAsStream.ReadByte());

我得到的(不想要的)结果是:

The (undesired) result I get is:

This:         M
Differs from: ?

看起来好像没有被正确读取,因为Message"的第一个字符是M",它在从 UnicodeEncoding 实例获取字节时起作用,但在从 UnicodeEncoding 实例中读取它们时不起作用流.

It looks like it's not being read correctly, as the first char of "Message" is 'M', which works when getting the bytes from the UnicodeEncoding instance but not when reading them back from the stream.

我做错了什么?

更大的图景:我有一个算法可以处理流的字节,我想尽可能通用并处理任何流.我想将 ASCII 字符串转换为 MemoryStream,或者使用另一种方法能够将字符串作为流处理.所讨论的算法将处理流的字节.

The bigger picture: I have an algorithm which will work on the bytes of a Stream, I'd like to be as general as possible and work with any Stream. I'd like to convert an ASCII-String into a MemoryStream, or maybe use another method to be able to work on the String as a Stream. The algorithm in question will work on the bytes of the Stream.

推荐答案

在你写入 MemoryStream 之后,在你读回来之前,你需要 Seek 回到MemoryStream 的开头,因此您不会从结尾阅读.

After you write to the MemoryStream and before you read it back, you need to Seek back to the beginning of the MemoryStream so you're not reading from the end.

更新

看到你的更新后,我认为有一种更可靠的方法来构建流:

After seeing your update, I think there's a more reliable way to build the stream:

UnicodeEncoding uniEncoding = new UnicodeEncoding();
String message = "Message";

// You might not want to use the outer using statement that I have
// I wasn't sure how long you would need the MemoryStream object    
using(MemoryStream ms = new MemoryStream())
{
    var sw = new StreamWriter(ms, uniEncoding);
    try
    {
        sw.Write(message);
        sw.Flush();//otherwise you are risking empty stream
        ms.Seek(0, SeekOrigin.Begin);

        // Test and work with the stream here. 
        // If you need to start back at the beginning, be sure to Seek again.
    }
    finally
    {
        sw.Dispose();
    }
}

如您所见,此代码使用 StreamWriter 将整个字符串(使用正确的编码)写入 MemoryStream.这样就无需确保写入字符串的整个字节数组.

As you can see, this code uses a StreamWriter to write the entire string (with proper encoding) out to the MemoryStream. This takes the hassle out of ensuring the entire byte array for the string is written.

更新:我多次遇到空流问题.写完后立即调用 Flush 就足够了.

Update: I stepped into issue with empty stream several time. It's enough to call Flush right after you've finished writing.

这篇关于将字符串写入流并将其读回不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:将字符串写入流并将其读回不起作用

基础教程推荐