Write text files without Byte Order Mark (BOM)?

86,680

Solution 1

In order to omit the byte order mark (BOM), your stream must use an instance of UTF8Encoding other than System.Text.Encoding.UTF8 (which is configured to generate a BOM). There are two easy ways to do this:

1. Explicitly specifying a suitable encoding:

  1. Call the UTF8Encoding constructor with False for the encoderShouldEmitUTF8Identifier parameter.

  2. Pass the UTF8Encoding instance to the stream constructor.

' 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. Using the default encoding:

If you do not supply an Encoding to StreamWriter's constructor at all, StreamWriter will by default use an UTF8 encoding without BOM, so the following should work just as well:

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

Finally, note that omitting the BOM is only permissible for UTF-8, not for UTF-16.

Solution 2

Try this:

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

Solution 3

Just Simply use the method WriteAllText from System.IO.File.

Please check the sample from File.WriteAllText.

This method uses UTF-8 encoding without a Byte-Order Mark (BOM), so using the GetPreamble method will return an empty byte array. If it is necessary to include a UTF-8 identifier, such as a byte order mark, at the beginning of a file, use the WriteAllText(String, String, Encoding) method overload with UTF8 encoding.

Solution 4

If you do not specify an Encoding when creating a new StreamWriter the default Encoding object used is UTF-8 No BOM which is created via new UTF8Encoding(false, true).

So to create a text file without the BOM use of of the constructors that do not require you to provide an encoding:

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

Solution 5

Interesting note with respect to this: strangely, the static "CreateText()" method of the System.IO.File class creates UTF-8 files without BOM.

In general this the source of bugs, but in your case it could have been the simplest workaround :)

Share:
86,680
VJOY
Author by

VJOY

I am a Senior Software Developer. Working for a reputed software company. I like to learn new things in life, be it from any perspective. You can connect me at, [email protected] http://www.whatilearnttuday.blogspot.com/

Updated on November 11, 2021

Comments

  • VJOY
    VJOY over 2 years

    I am trying to create a text file using VB.Net with UTF8 encoding, without BOM. Can anybody help me, how to do this?
    I can write file with UTF8 encoding but, how to remove Byte Order Mark from it?

    edit1: I have tried code like this;

        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 get created with UTF8 encoding only and 2.html get created with ANSI encoding format.

    Simplified approach - http://whatilearnttuday.blogspot.com/2011/10/write-text-files-without-byte-order.html

  • Muflix
    Muflix about 9 years
    On my PC it creates ANSI files
  • binki
    binki over 8 years
    What if I need to specify leaveOpen?
  • JG in SD
    JG in SD over 8 years
    @binki in that case you can not use the default encoding that StreamWriter uses. You'll need to specify new UTF8Encoding(false, true) for your encoding to be able to specify leaveOpen and not have the BOM.
  • beppe9000
    beppe9000 about 8 years
    The one from the My namespace does use BOM
  • beppe9000
    beppe9000 almost 8 years
    Not always wise: for example My.Computer.FileSystem.WriteAllText writes the BOM if no encoding is specified.
  • jnm2
    jnm2 almost 8 years
    My.Computer.FileSystem.WriteAllText is an exception in this regard, guessing for backwards VB compatibility perhaps? File.WriteAllText defaults to UFT8NoBOM.
  • PeterCo
    PeterCo over 3 years
    This is especially helpful if you want to write a *.m3u8 playlist file for VLC. VLC is still not capable to read UTF8 playlist files WITH BOM! This seems to be fixed according to trac.videolan.org/vlc/ticket/21860, but will only be included in VLC v4.