我有一个StreamWriter
在我读取它的同时打开我的文件,这似乎是导致问题的原因(这是更大的一组代码中的一个较小片段,只是以说明我的问题):StreamWriter正在将BOM字符65279附加到文件结尾
static void Main(string[] args)
{
for (int i = 0; i < 3; i++)
{
using (FileStream stream = new FileStream("file.txt", FileMode.OpenOrCreate))
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8, false, 0x1000, true))
using (StreamWriter writer = new StreamWriter(stream, Encoding.UTF8, 0x1000, true))
{
Console.WriteLine("Read \"" + reader.ReadToEnd() + "\" from the file.");
}
}
Console.ReadLine();
}
上面的代码将输出:
Read "" from the file.
Read "" from the file.
Read "?" from the file.
如果文件已经包含了一些文字,笔者将BOM到最后追加尽管从未被称为写任何东西:
Read "TEXT" from the file.
Read "TEXT?" from the file.
Read "TEXT??" from the file.
它为什么会表现出这种行为?
可能的重复[如何忽略字符串比较中的UTF-8字节顺序标记?](http://stackoverflow.com/questions/2915182/how-do-i-ignore-the-utf-8-字节顺序标记在字符串比较) – grovesNL 2014-12-04 16:35:39
@grovesNL这是关于StreamReader,而不是关于GetString,这些答案不帮助我。 – Alexandru 2014-12-04 16:41:51
@grovesNL即使它的BOM值我会很惊讶地看到**在最后**而不是开始... – 2014-12-04 16:41:59