栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

查找任何文件编码的有效方法

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

查找任何文件编码的有效方法

StreamReader.CurrentEncoding
属性很少为我返回正确的文本文件编码。通过分析文件的字节序标记(BOM),我在确定文件的字节序方面取得了更大的成功。如果文件没有BOM,则无法确定文件的编码。

*已更新4/08/2020,包括UTF-32LE检测并返回UTF-32BE的正确编码

/// <summary>/// Determines a text file's encoding by analyzing its byte order mark (BOM)./// Defaults to ASCII when detection of the text file's endianness fails./// </summary>/// <param name="filename">The text file to analyze.</param>/// <returns>The detected encoding.</returns>public static Encoding GetEncoding(string filename){    // Read the BOM    var bom = new byte[4];    using (var file = new FileStream(filename, FileMode.Open, FileAccess.Read))    {        file.Read(bom, 0, 4);    }    // Analyze the BOM    if (bom[0] == 0x2b && bom[1] == 0x2f && bom[2] == 0x76) return Encoding.UTF7;    if (bom[0] == 0xef && bom[1] == 0xbb && bom[2] == 0xbf) return Encoding.UTF8;    if (bom[0] == 0xff && bom[1] == 0xfe && bom[2] == 0 && bom[3] == 0) return Encoding.UTF32; //UTF-32LE    if (bom[0] == 0xff && bom[1] == 0xfe) return Encoding.Unipre; //UTF-16LE    if (bom[0] == 0xfe && bom[1] == 0xff) return Encoding.BigEndianUnipre; //UTF-16BE    if (bom[0] == 0 && bom[1] == 0 && bom[2] == 0xfe && bom[3] == 0xff) return new UTF32Encoding(true, true);  //UTF-32BE    // We actually have no idea what the encoding is if we reach this point, so    // you may wish to return null instead of defaulting to ASCII    return Encoding.ASCII;}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/514737.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号