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

我想创build一个文本文件使用VB.Net与UTF8编码,没有BOM。 任何人都可以帮助我,怎么做?
我可以使用UTF8编码编写文件,但是,如何从中删除字节顺序标记?

编辑1:我已经尝试过这样的代码;

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编码创build,2.html使用ANSI编码格式创build。

简化方法 – http://whatilearnttuday.blogspot.com/2011/10/write-text-files-without-byte-order.html

为了省略字节顺序标记(BOM),您的stream必须使用除System.Text.Encoding.UTF8 (configuration为生成BOM)之外的UTF8Encoding实例。 有两个简单的方法来做到这一点:

1.明确指定一个合适的编码:

  1. encoderShouldEmitUTF8Identifier参数调用UTF8Encoding构造函数的False

  2. UTF8Encoding实例传递给stream构造函数。

 ' VB.NET: Dim utf8WithoutBom As New System.Text.UTF8Encoding(False) Using sink As New StreamWriter("Foobar.txt", False, utf8WithoutBom) sink.WriteLine("...") End Using 
 // C#: var utf8WithoutBom = new System.Text.UTF8Encoding(false); using (var sink = new StreamWriter("Foobar.txt", false, utf8WithoutBom)) { sink.WriteLine("..."); } 

2.使用默认编码:

如果您根本不提供EncodingStreamWriter的构造函数, StreamWriter将默认使用不带BOM的UTF8编码,因此以下内容也应该可以正常工作:

 ' VB.NET: Using sink As New StreamWriter("Foobar.txt") sink.WriteLine("...") End Using 
 // C#: using (var sink = new StreamWriter("Foobar.txt")) { sink.WriteLine("..."); } 

最后,请注意,只有UTF-8才允许省略BOM,而不是UTF-16。

尝试这个:

 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 

只需使用System.IO.File的方法WriteAllText

请从File.WriteAllText检查示例。

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

有趣的一点是:奇怪的是,System.IO.File类的静态“CreateText()”方法创build了没有 BOM的UTF-8文件。

一般来说这是错误的来源,但在你的情况下,它可能是最简单的解决方法:)

如果您在创build新的StreamWriter时未指定Encoding ,则使用的默认Encoding对象是通过new UTF8Encoding(false, true)创build的UTF-8 No BOM

所以要创build一个文本文件,而不需要使用不需要提供编码的构造函数的BOM使用:

 new StreamWriter(Stream) new StreamWriter(String) new StreamWriter(String, Boolean) 

我认为罗马尼基丁是正确的。 构造函数参数的含义被翻转。 假意味着没有BOM,真正意味着BOM。

您得到一个ANSI编码,因为没有不包含非ANSI字符的BOM的文件是完全相同的ANSI文件。 在你的“hi there”string中尝试一些特殊字符,你会看到ANSI编码改变为无BOM。

XML编码没有BOM的UTF-8
我们需要将XML数据提交给EPA,他们的应用程序需要我们的input需要UTF-8没有BOM。 哦,是的,普通的UTF-8应该是可以接受的,但是不适用于EPA。 上述评论的答案就是这样。 谢谢罗马Nikitin

以下是XML编码的代码的C#代码片段:

  Encoding utf8noBOM = new UTF8Encoding(false); XmlWriterSettings settings = new XmlWriterSettings(); settings.Encoding = utf8noBOM; … using (XmlWriter xw = XmlWriter.Create(filePath, settings)) { xDoc.WriteTo(xw); xw.Flush(); } 

要看看这是否真的从输出文件中删除三个主angular字符可能会误导。 例如,如果您使用Notepad ++ (www.notepad-plus-plus.org),它将报告“ANSI编码”。 我猜大多数文本编辑器都依靠BOM字符来判断它是否是UTF-8。 清楚地看到这一点的方法是使用像WinHex (www.winhex.com)这样的二进制工具。 由于我在寻找前后差异,我使用了Microsoft WinDiff应用程序。

这可能是您的input文本包含一个字节顺序标记。 在这种情况下,你应该在写之前将其删除。

 Dim sWriter As IO.StreamWriter = New IO.StreamWriter(shareworklist & "\" & getfilename() & ".txt", False, Encoding.Default) 

给你结果作为你想要的(我认为)。