119

BOM なしで、UTF8 エンコーディングの VB.Net を使用してテキスト ファイルを作成しようとしています。誰でも私を助けることができますか、これを行う方法は?
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

4

10 に答える 10

206

バイト オーダー マーク (BOM) を省略するには、ストリームでUTF8Encodingother than System.Text.Encoding.UTF8(BOM を生成するように構成されている) のインスタンスを使用する必要があります。これを行うには、次の 2 つの簡単な方法があります。

1. 適切なエンコーディングを明示的に指定する:

  1. パラメータのUTF8Encodingコンストラクタを呼び出します。FalseencoderShouldEmitUTF8Identifier

  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. デフォルトのエンコーディングを使用:

EncodingtoStreamWriterのコンストラクタをまったく指定しない場合、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-16 ではなく UTF-8 でのみ許可されていることに注意してください。

于 2010-03-13T08:49:54.860 に答える
29

これを試して:

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
于 2010-05-07T12:18:23.510 に答える
6

WriteAllTextからのメソッドを使用するだけSystem.IO.Fileです。

File.WriteAllTextのサンプルを確認してください。

このメソッドはバイト オーダー マーク (BOM) なしで UTF-8 エンコードを使用するため、GetPreamble メソッドを使用すると空のバイト配列が返されます。ファイルの先頭にバイト オーダー マークなどの UTF-8 識別子を含める必要がある場合は、UTF8 エンコーディングで WriteAllText(String, String, Encoding) メソッドのオーバーロードを使用します。

于 2014-10-14T06:26:46.680 に答える
4

Encoding新しい を作成するときに を指定しない場合、使用されるStreamWriterデフォルトEncodingオブジェクトはUTF-8 No BOM、 を介して作成されたものですnew UTF8Encoding(false, true)

したがって、エンコーディングを提供する必要のないコンストラクターの BOM を使用せずにテキスト ファイルを作成するには、次のようにします。

new StreamWriter(Stream)
new StreamWriter(String)
new StreamWriter(String, Boolean)
于 2015-06-23T20:46:14.683 に答える
4

これに関する興味深い注意: 奇妙なことに、System.IO.File クラスの静的な "CreateText()" メソッドは、BOMなしでUTF-8 ファイルを作成します。

一般に、これはバグの原因ですが、あなたの場合は最も簡単な回避策でした:)

于 2011-04-14T08:21:09.783 に答える
-1
Dim sWriter As IO.StreamWriter = New IO.StreamWriter(shareworklist & "\" & getfilename() & ".txt", False, Encoding.Default)

希望どおりの結果が得られます(と思います)。

于 2011-12-22T05:41:57.293 に答える
-1

入力テキストにバイト オーダー マークが含まれている可能性があります。その場合は、書き込む前に削除してください。

于 2010-03-13T08:52:46.353 に答える