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

我正在尝试使用带有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


凤凰求蛊
浏览 476回答 3
3回答

当年话下

尝试这个:Encoding outputEnc = new UTF8Encoding(false); // create encoding with no BOMTextWriter file = new StreamWriter(filePath, false, outputEnc); // open file with encoding// write data herefile.Close(); // save and close it

慕婉清6462132

只需简单地使用的方法WriteAllText从System.IO.File。请检查File.WriteAllText中的示例。此方法使用不带字节序标记(BOM)的UTF-8编码,因此使用GetPreamble方法将返回一个空字节数组。如果必须在文件的开头包含UTF-8标识符(例如字节顺序标记),请使用带有UTF8编码的WriteAllText(String,String,Encoding)方法重载。
打开App,查看更多内容
随时随地看视频慕课网APP