简体   繁体   English

VB Streamwriter设置为ISO-8859-1,但文件获取UTF8-无Bom编码

[英]VB Streamwriter set to ISO-8859-1 but file get UTF8-Without Bom encoding

I have a problem i set the streawriter to write ISO-8853-1 but the file gets utf8 encoding. 我有一个问题,我将Streawriter设置为写入ISO-8853-1,但是文件获取utf8编码。 See code below: 参见下面的代码:

when declaring str i set it to iso-8853-1 and when i check the output files encoding i get UTF8-without bom. 当声明str时,我将其设置为iso-8853-1,当我检查编码的输出文件时,我得到的是UTF8,没有bom。

Imports System.Text
Imports System.IO

Module Module1

Sub Main()
    Using str = New StreamWriter("C:\blabla.txt", False, encoding.GetEncoding("ISO-8859-1"))
        str.Write("23124124AÖ")
        Dim encoding = str.Encoding
        str.Close()
    End Using

    Dim currentEncoding = GetFileEncoding("C:\blabla.txt")
End Sub
    Public Function GetFileEncoding(filePath As String) As Encoding
        Using sr As StreamReader = New StreamReader(filePath, True)
            sr.Read()
            Return sr.CurrentEncoding
        End Using
    End Function
End Module

There's a reason that you have to pass encodings to the constructors of both readers and writers. 有一个原因,您必须将编码传递给读者和作家的构造函数。 Files, in and of themselves, don't have encodings. 文件,在和自己, 没有编码。 They're just a collection of bytes. 它们只是字节的集合。

It's up to you , be selecting an encoding, to say what interpretation you want to place on those bytes. 您自己决定选择一种编码,说出您想在这些字节上放置什么解释。 You opened the StreamReader without specifying an encoding and so it defaults to UTF-8. 您在打开StreamReader未指定编码,因此它默认为UTF-8。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM