【问题标题】:How to use ReadAllText when file encoding unknown文件编码未知时如何使用 ReadAllText
【发布时间】:2012-05-31 23:27:15
【问题描述】:

我正在使用 ReadAllText 读取文件

    String[] values = File.ReadAllText(@"c:\\c\\file.txt").Split(';');

    int i = 0;

    foreach (String s in values)
    {
        System.Console.WriteLine("output: {0} {1} ", i, s);
        i++;
    }

如果我尝试读取某些文件,有时会返回错误的字符(对于 ÖÜÄÀ...)。输出类似'?',是因为编码有问题:

output: 0 TEST
output: 1 A??O?

一种解决方案是在 ReadAllText 中设置编码,比如ReadAllText(@"c:\\c\\file.txt", Encoding.UTF8) 这样可以解决问题。但如果我仍然会得到'?作为输出?如果我不知道文件的编码怎么办?如果每个文件都有不同的编码怎么办?用 c# 做这件事的最好方法是什么?谢谢

【问题讨论】:

标签: c# .net encoding


【解决方案1】:

可靠地做到这一点的唯一方法是在文本文件的开头查找byte order marks。 (这个 blob 更普遍地表示使用的字符编码的字节顺序,但也表示编码 - 例如 UTF8、UTF16、UTF32)。不幸的是,这种方法仅适用于基于 Unicode 的编码,在此之前没有任何效果(必须使用可靠性要低得多的方法)。

StreamReader 类型支持检测这些标记以确定编码 - 您只需将标志传递给参数:

new System.IO.StreamReader("path", true)

然后您可以检查stremReader.CurrentEncoding 的值以确定文件使用的编码。但请注意,如果不存在字节编码标记,则 CurrentEncoding 将默认为 Encoding.Default

Refer codeproject solution to detect encoding

【讨论】:

  • 如果不存在字节编码标记,则 CurrentEncoding 将使用 Encoding.UTF8 not Encoding.Default。 “detectEncodingFromByteOrderMarks 参数通过查看流的前三个字节来检测编码。如果文件以适当的字节顺序标记开头,它会自动识别 UTF-8、little-endian Unicode 和 big-endian Unicode 文本。否则,使用 UTF8 编码。” from the docs
【解决方案2】:

您必须先检查文件编码。试试这个

System.Text.Encoding enc = null; 
System.IO.FileStream file = new System.IO.FileStream(filePath, 
    FileMode.Open, FileAccess.Read, FileShare.Read); 
if (file.CanSeek) 
{ 
    byte[] bom = new byte[4]; // Get the byte-order mark, if there is one 
    file.Read(bom, 0, 4); 
    if ((bom[0] == 0xef && bom[1] == 0xbb && bom[2] == 0xbf) || // utf-8 
        (bom[0] == 0xff && bom[1] == 0xfe) || // ucs-2le, ucs-4le, and ucs-16le 
        (bom[0] == 0xfe && bom[1] == 0xff) || // utf-16 and ucs-2 
        (bom[0] == 0 && bom[1] == 0 && bom[2] == 0xfe && bom[3] == 0xff)) // ucs-4 
    { 
        enc = System.Text.Encoding.Unicode; 
    } 
    else 
    { 
        enc = System.Text.Encoding.ASCII; 
    } 

    // Now reposition the file cursor back to the start of the file 
    file.Seek(0, System.IO.SeekOrigin.Begin); 
} 
else 
{ 
    // The file cannot be randomly accessed, so you need to decide what to set the default to 
    // based on the data provided. If you're expecting data from a lot of older applications, 
    // default your encoding to Encoding.ASCII. If you're expecting data from a lot of newer 
    // applications, default your encoding to Encoding.Unicode. Also, since binary files are 
    // single byte-based, so you will want to use Encoding.ASCII, even though you'll probably 
    // never need to use the encoding then since the Encoding classes are really meant to get 
    // strings from the byte array that is the file. 

    enc = System.Text.Encoding.ASCII; 
}

【讨论】:

  • 谢谢!请注意,在此代码之后 FileStream 是打开的,如果此代码用于某些 GetEncoding 方法,则应关闭。
【解决方案3】:

在我的例子中,我正在创建一些简单的 json 文件并且遇到了同样的错误。 问题是使用 Visual Studio(目前是 2019 年)创建文件。

我相信你可以在 VS 选项中找到一些配置来处理这个问题。但是,我发现最快的方法是使用 Notepad++ 创建相同的文件和内容。 您可以通过访问编码顶部菜单在 Notepad++ 中设置编码。 而且我相信你也可以在其他文本编辑器中找到类似的配置。

【讨论】:

    猜你喜欢
    • 2021-04-29
    • 1970-01-01
    • 2018-03-05
    • 2015-03-20
    • 2014-08-17
    • 2015-03-12
    • 2017-01-21
    相关资源
    最近更新 更多