原文参考http://www.19870202.com/?tid=381
Q: What is a BOM?
A: UTF-8 编码的文件可以分为no BOM 和 BOM两种格式。
何谓BOM? "EF BB BF" 这三个字节就叫BOM,BOM的全称叫做"Byte Order Mard".在utf-8文件中常用BOM来表明这个文件是UTF-8文件,而BOM的本意实在utf16中用来表示高低字节序列的。
在字节流之前有BOM表示采用低字节序列(低字节在前面),而utf8不用考虑字节序列,所以其实有无BOM都可以。
UTF8Encoding.GetPreamble方法:
Code
1 public override byte[] GetPreamble()
2 {
3 if (this.emitUTF8Identifier)
4 {
5 return new byte[] { 0xef, 0xbb, 0xbf };
6 }
7 return Encoding.emptyByteArray;
8 }
1 public override byte[] GetPreamble()
2 {
3 if (this.emitUTF8Identifier)
4 {
5 return new byte[] { 0xef, 0xbb, 0xbf };
6 }
7 return Encoding.emptyByteArray;
8 }
用以下方法就可以去掉BOM签名
Code
System.Text.UTF8Encoding utf8 = new System.Text.UTF8Encoding(false);
StreamWriter stream = new StreamWriter(Server.MapPath("normren.html"), false,utf8);
stream.Write("Content");
stream.Close();
System.Text.UTF8Encoding utf8 = new System.Text.UTF8Encoding(false);
StreamWriter stream = new StreamWriter(Server.MapPath("normren.html"), false,utf8);
stream.Write("Content");
stream.Close();