代码之家  ›  专栏  ›  技术社区  ›  VJOY

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

  •  113
  • VJOY  · 技术社区  · 16 年前


    我可以写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

    9 回复  |  直到 10 年前
        1
  •  203
  •   stakx - no longer contributing Saravana Kumar    10 年前

    为了省略字节顺序标记(byte order mark,BOM),流必须使用 UTF8Encoding System.Text.Encoding.UTF8 (配置为生成BOM表)。有两种简单的方法:

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

    1. 打电话给 UTF8Encoding constructor 具有 False 对于 encoderShouldEmitUTF8Identifier

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

    ' 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("...");
    }
    

    如果你不提供 Encoding 到 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("...");
    }
    

    最后,请注意,省略BOM只允许用于UTF-8,而不允许用于UTF-16。

        2
  •  28
  •   Roman Nikitin    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
    
        3
  •  6
  •   Joe.wang    12 年前

    只是简单地使用这个方法 WriteAllText 从…起 System.IO.File

    请核对样品 File.WriteAllText .

    这种方法使用UTF-8编码,没有字节顺序标记(BOM),因此 必须包括UTF-8标识符,例如字节顺序标记 在文件的开头,使用WriteAllText(String,String,

        4
  •  4
  •   Tao    15 年前

    有趣的是:奇怪的是,System.IO.File类的静态“CreateText()”方法创建UTF-8文件 没有 物料清单。

        5
  •  4
  •   JG in SD    9 年前

    如果不指定 Encoding 创建新的 StreamWriter 编码 UTF-8 No BOM 它是通过 new UTF8Encoding(false, true) .

    因此,要在不使用BOM表的情况下创建文本文件,请使用不需要您提供编码的构造函数:

    new StreamWriter(Stream)
    new StreamWriter(String)
    new StreamWriter(String, Boolean)
    
        6
  •  3
  •   jos    12 年前

    获得ANSI编码是因为没有包含非ANSI字符的BOM表的文件与ANSI文件完全相同。在“hi there”字符串中尝试一些特殊字符,您将看到ANSI编码更改为“without BOM”。

        7
  •  1
  •   Jerry Banasik    10 年前


    我们需要向EPA提交XML数据,而他们接受我们输入的应用程序需要不带BOM的UTF-8。哦,是的,普通的UTF-8应该是每个人都可以接受的,但不是环保局。答案在上面的评论中。非常感谢。 .

        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公司 温迪夫

        8
  •  -1
  •   user180326 user180326    16 年前

    可能是输入文本包含字节顺序标记。那样的话,你应该在写之前把它去掉。

        9
  •  -1
  •   Mark Hall    14 年前
    Dim sWriter As IO.StreamWriter = New IO.StreamWriter(shareworklist & "\" & getfilename() & ".txt", False, Encoding.Default)
    

    给你想要的结果(我想)。

    推荐文章