为了账号安全,请及时绑定邮箱和手机立即绑定

写没有字节序标记(BOM)的文本文件吗?

写没有字节序标记(BOM)的文本文件吗?

凤凰求蛊 2019-12-27 15:01:41
我正在尝试使用带有UTF8编码的VB.Net创建文本文件,而没有BOM。谁能帮我,怎么做?我可以使用UTF8编码写入文件,但是如何从其中删除字节顺序标记?edit1:我尝试过这样的代码;    Dim utf8 As New UTF8Encoding()    Dim utf8EmitBOM As New UTF8Encoding(True)    Dim strW As New StreamWriter("c:\temp\bom\1.html", True, utf8EmitBOM)    strW.Write(utf8EmitBOM.GetPreamble())    strW.WriteLine("hi there")    strW.Close()        Dim strw2 As New StreamWriter("c:\temp\bom\2.html", True, utf8)        strw2.Write(utf8.GetPreamble())        strw2.WriteLine("hi there")        strw2.Close()1.html仅使用UTF8编码创建,而2.html使用ANSI编码格式创建。简化方法-http: //whatilearnttuday.blogspot.com/2011/10/write-text-files-without-byte-order.html
查看完整描述

3 回答

?
当年话下

TA贡献1890条经验 获得超9个赞

尝试这个:


Encoding outputEnc = new UTF8Encoding(false); // create encoding with no BOM

TextWriter file = new StreamWriter(filePath, false, outputEnc); // open file with encoding

// write data here

file.Close(); // save and close it


查看完整回答
反对 回复 2019-12-27
?
慕婉清6462132

TA贡献1804条经验 获得超2个赞

只需简单地使用的方法WriteAllText从System.IO.File。


请检查File.WriteAllText中的示例。


此方法使用不带字节序标记(BOM)的UTF-8编码,因此使用GetPreamble方法将返回一个空字节数组。如果必须在文件的开头包含UTF-8标识符(例如字节顺序标记),请使用带有UTF8编码的WriteAllText(String,String,Encoding)方法重载。


查看完整回答
反对 回复 2019-12-27
  • 3 回答
  • 0 关注
  • 475 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信