• .netcore 读取ansi编码


    public class FileHelper
        {
            //根据文件自动觉察编码并输出内容
           public static string GetText(string path)
            {
                StringBuilder result = new StringBuilder();
                var enc = GetEncoding(path, Encoding.GetEncoding("gb2312"));
                using (var sr = new StreamReader(path, enc))
                {
                    result.Append(sr.ReadToEnd());
                }
                return result.ToString();
            }
    
            /// <summary>
            /// 根据文件尝试返回字符编码
            /// </summary>
            /// <param name="file">文件路径</param>
            /// <param name="defEnc">没有BOM返回的默认编码</param>
            /// <returns>如果文件无法读取,返回null。否则,返回根据BOM判断的编码或者缺省编码(没有BOM)。</returns>
            static Encoding GetEncoding(string file, Encoding defEnc)
            {
                using (var stream = File.OpenRead(file))
                {
                    //判断流可读?
                    if (!stream.CanRead)
                        return null;
                    //字节数组存储BOM
                    var bom = new byte[4];
                    //实际读入的长度
                    int readc;
    
                    readc = stream.Read(bom, 0, 4);
    
                    if (readc >= 2)
                    {
                        if (readc >= 4)
                        {
                            //UTF32,Big-Endian
                            if (CheckBytes(bom, 4, 0x00, 0x00, 0xFE, 0xFF))
                                return new UTF32Encoding(true, true);
                            //UTF32,Little-Endian
                            if (CheckBytes(bom, 4, 0xFF, 0xFE, 0x00, 0x00))
                                return new UTF32Encoding(false, true);
                        }
                        //UTF8
                        if (readc >= 3 && CheckBytes(bom, 3, 0xEF, 0xBB, 0xBF))
                            return new UTF8Encoding(true);
    
                        //UTF16,Big-Endian
                        if (CheckBytes(bom, 2, 0xFE, 0xFF))
                            return new UnicodeEncoding(true, true);
                        //UTF16,Little-Endian
                        if (CheckBytes(bom, 2, 0xFF, 0xFE))
                            return new UnicodeEncoding(false, true);
                    }
    
                    return defEnc;
                }
            }
    
            //辅助函数,判断字节中的值
            static bool CheckBytes(byte[] bytes, int count, params int[] values)
            {
                for (int i = 0; i < count; i++)
                    if (bytes[i] != values[i])
                        return false;
                return true;
            }
        }

    调用

    //首先注册编码提供程序
    Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
    //调用
    var str=FileHelper.GetText(path);
  • 相关阅读:
    HTML基础学习笔记
    CSS-精灵图片的使用(从一张图片中截图指定位置图标)
    侧边栏显示
    HTML <form> action 属性
    2018年寒假小记
    算法提高--接水问题
    基础练习--huffman
    ...
    基础算法
    枚举--最长单词--蓝桥杯
  • 原文地址:https://www.cnblogs.com/ives/p/10346498.html
Copyright © 2020-2023  润新知