为什么这不会将正确的字节写入文件?
问题描述:
这里是我的测试写入文件:为什么这不会将正确的字节写入文件?
[Test]
public void CanWriteManifestToFile()
{
byte[] data = new byte[] { 0x00, 0x01, 0x80, 0x1f };
MemoryStream ms = new MemoryStream(data);
var mg = new ManifestGeneratorV1();
mg.WriteManifestFile(ms, "TestManifest.mnf");
Assert.IsTrue(File.Exists("TestManifest.mnf"));
Assert.AreEqual(data, GetDataFromFile("TestManifest.mnf"));
}
这里是WriteManifestFile方法实际执行写:
public void WriteManifestFile(MemoryStream ms, string filePath)
{
using (StreamWriter sw = new StreamWriter(filePath, false))
{
ms.Seek(0, 0);
using (StreamReader sr = new StreamReader(ms))
{
sw.Write(sr.ReadToEnd());
}
}
}
我的测试失败。结果是以下字节数组{00,01,ef,bf,bd,1f}
。现在,如果我将80更改为不以f
或8
开头的内容,则一切正常。什么可能导致80
变为efbfbd
?
答
您正在使用串对非字符串数据的方法; ReadToEnd
和Write(string)
。这是无效的;腐败是这种直接结果(即通过文本Encoding
运行任意数据)。使用原始Stream
API来代替:
using(var file = File.Create(filePath))
{
ms.Position = 0;
ms.CopyTo(file);
}
或者只是:
File.WriteAllBytes(filePath, ms.ToArray());
答
StreamReader.ReadToEnd()
返回一个字符串。这意味着它需要解释它读取的流中的字节。对于这种解释,它使用了一种编码,我猜你的情况是使用UTF-8。这是错误的,因为你的字节不代表文本。
你确实想读取字节并将它们写入文件而没有任何解释。像这样的东西。
var bytes = new byte[ms.Length];
ms.Read(bytes, 0, bytes.Length);
using(var fileStream = new FileStream(...))
{
fileStream.Write(bytes, 0, bytes.Length);
}
你忘了说你正在改变什么使它正常工作。 –
这些字节是utf8 BOM。如果你真的想用BOM(可疑)编写utf8,那么使用新的UTF8Encoding(false)。 –