首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

保存文件的时候,如何添加BOM

2012-06-12 
保存文件的时候,怎么添加BOMC# codeusing (StreamWriter sw new StreamWriter(abc.txt)){sw.Write(str

保存文件的时候,怎么添加BOM

C# code
using (StreamWriter sw = new StreamWriter("abc.txt")){         sw.Write(str);          sw.Close();}


上面的代码,默认以UTF-8编码,来保存str。

但我发现,并没有带上BOM。

怎么加上UTF-8的BOM?

[解决办法]
手动在头上加上ef bb bf这么三个字节?
[解决办法]
C# code
using (Stream stream = File.Open("E:\\abc.txt", FileMode.Create))using (StreamWriter sw = new StreamWriter(stream)){    stream.WriteByte(Convert.ToByte("EF", 16));    stream.WriteByte(Convert.ToByte("BB", 16));    stream.WriteByte(Convert.ToByte("BF", 16));    sw.Write(str);} 

热点排行