我正在尝试使用 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 编码创建,2.html 使用 ANSI 编码格式创建。

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

有帮助吗?

解决方案

为了省略字节顺序标记 (BOM),您的流必须使用 UTF8Encoding 以外 System.Text.Encoding.UTF8 (配置为生成 BOM)。有两种简单的方法可以做到这一点:

1.显式指定合适的编码:

  1. 致电 UTF8Encoding 构造函数False 为了 encoderShouldEmitUTF8Identifier 范围。

  2. 通过 UTF8Encoding 流构造函数的实例。

' 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

相对于该有趣注:奇怪的是,静态 “CreateText()” 有System.IO.File类的方法创建UTF-8文件的没有 BOM

在此一般错误的根源,但在你的情况下,它可能是最简单的解决办法:)

如果您不指定 Encoding 当创建一个新的 StreamWriter 默认值 Encoding 使用的对象是 UTF-8 No BOM 这是通过创建的 new UTF8Encoding(false, true).

因此,要创建不带 BOM 的文本文件,请使用不需要提供编码的构造函数:

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

我觉得罗马尼基京是正确的。构造函数参数的含义翻转。假手段没有BOM和真装置与BOM。

您得到一个ANSI编码的,因为没有一个不包含非ANSI字符的BOM文件是完全一样的ANSI文件。尝试一些特殊的字符在你“嗨”的字符串,你会看到ANSI编码的改变不-BOM。

<强> XML编码UTF-8无BOM 结果 我们需要将XML数据提交给EPA和他们的应用程序,它需要我们的输入需要UTF-8无BOM。哦,是的,简单的UTF-8应该是大家可以接受的,但不是为EPA。这个问题的答案这样做是在上述评论。感谢您的罗马尼基

下面是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();  
    }    

要看看是否这实际上消除从输出文件中的三个主要字符可能会产生误导。例如,如果使用的记事本++ (www.notepad-plus-plus.org),它将报告“编码在ANSI”。我想大多数文本编辑器都指望BOM字符,判断它是UTF-8。清楚地看到这一点的方法是用象一个二进制工具的的WinHex (www.winhex.com)。由于我一直在寻找一个之前和之后的区别我用微软的 WinDiff的应用程序。

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

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

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top