关于C# 在TXT写入数据使用UTF-8 bom编码模式

时间:2014-10-31 11:50:40   收藏:0   阅读:1790

C#中通过

1 StreamWriter sw = new StreamWriter(path, true);
2 sw.WriteLine(Content);

这样的代码写入TXT中的数据默认的编码是GB2312格式,那么如何才能使用utf-8的编码格式写入TXT呢?

代码如下:

1 UTF8Encoding utf8 = new UTF8Encoding(false);
2 StreamWriter sw = new StreamWriter(path, true, utf8);

这样确实使用utf-8的编码写入没错,但是却不是BOM的编码模式,那如何才能使用utf-8 BOM编码格式呢?

其实很简单,代码如下:

1 UTF8Encoding utf8BOM = new UTF8Encoding(true);
2  StreamWriter sw = new StreamWriter(path, true, utf8BOM);

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!