SORU
13 Mart 2010, CUMARTESİ


Bayt Sırası İşareti (BOM) olmadan metin dosyaları yazmak?

Bir metin dosyasını UTF-8 kodlaması ile VB.Net kullanarak, BOM olmadan oluşturmak için çalışıyorum. Kimse bunu bana nasıl yardımcı olabilir?
Ondan Bayt Sırası İşareti kaldırmak için nasıl UTF8 kodlama ile dosya ama yazabilir miyim?
Şimdiden teşekkürler. edit1: Bu kodu denedim;

    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 kodlama ile sadece oluşturulan ve 2.html ANSI kodlama biçimi ile oluşturulan.

Basitleştirilmiş yaklaşım http://whatilearnttuday.blogspot.com/2011/10/write-text-files-without-byte-order.html

CEVAP
13 Mart 2010, CUMARTESİ


Bayt sırası işareti (BOM) ihmal etmek amacıyla, akışı 5* *varsayılan yerine UTF8Encoding özel bir örneğini kullanmanız gerekir:

  1. encoderShouldEmitUTF8Identifier parametre False UTF8Encoding constructor Ara.

  2. Stream kurucu UTF8Encoding örneğini geçirin.

Örnek (VB.NET):

Dim utf8WithoutBom As New System.Text.UTF8Encoding(False)
                                                  '^^^^^'
Using sink As New StreamWriter("Foobar.txt", False, utf8WithoutBom)
    sink.WriteLine("...")
End Using

(C#) Örnek:

var utf8WithoutBom = new System.Text.UTF8Encoding(false);
                                                //^^^^^
using (var sink = new StreamWriter("Foobar.txt", false, utf8WithoutBom))
{
    sink.WriteLine("...");
}

BOM atlama UTF-16, UTF-8, sadece caiz olduğunu unutmayın.

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Dellbear816

    Dellbear816

    4 Mart 2008
  • HowtoDrawAndPaint

    HowtoDrawAnd

    24 EKİM 2010
  • UlyssesForever's channel

    UlyssesForev

    28 ŞUBAT 2012