.net 各种文件编码方式读取txt文件

txt文件,有不同的编码方式,导入到系统有时不识别,故做以下适配。

#region 字节流编码格式判断
        /// <summary>
        /// 获取txt文件内容行
        /// 兼容不同的编码方式
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="isTrim">是否去除行两边空白字符</param>
        /// <param name="isBlank">是否包含空白行</param>
        /// <returns></returns>
        public static List<string> GetLinesFromTxt(Stream stream, bool isTrim = true, bool isBlank = false)
        {
            #region 读取txt文件
            List<string> paragraphList = new List<string>();
            var encoding = SowerPower.Common.Utils.GetStreamEncoding(stream);
            using (StreamReader sr = new StreamReader(stream, encoding))
            {
                string lineTxt = "";
                while ((lineTxt = sr.ReadLine()) != null)
                {
                    var trimTxt = lineTxt.Trim();
                    if (!isBlank && trimTxt.Length == 0)
                    {
                        continue;
                    }
                    if (isTrim) paragraphList.Add(trimTxt);
                    else paragraphList.Add(lineTxt);
                }

            }
            //sr.Close();
            //sr.Dispose();
            return paragraphList;
            #endregion
        }
        /// <summary>
        /// 判断获取字节流 编码格式,主要用于txt文件内容读取
        /// </summary>
        /// <param name="fs"></param>
        /// <returns></returns>
        public static Encoding GetStreamEncoding(Stream fs)
        {
            fs.Position = 0;
            if (fs.Length < 2) return System.Text.Encoding.Default;
            System.IO.BinaryReader br = new System.IO.BinaryReader(fs);
            Byte[] buffer = br.ReadBytes(2);
            fs.Position = 0;
            if (buffer[0] >= 0xEF)
            {
                if (buffer[0] == 0xEF && buffer[1] == 0xBB)
                {
                    return System.Text.Encoding.UTF8;
                }
                else if (buffer[0] == 0xFE && buffer[1] == 0xFF)
                {
                    return System.Text.Encoding.BigEndianUnicode;
                }
                else if (buffer[0] == 0xFF && buffer[1] == 0xFE)
                {
                    return System.Text.Encoding.Unicode;
                }
                else
                {
                    return System.Text.Encoding.Default;
                }
            }
            else
            {

                fs.Position = 0;
                br = new System.IO.BinaryReader(fs);
                buffer = br.ReadBytes((int)fs.Length);
                fs.Position = 0;
                ///判断是否不带bom的utf8格式
                if (IsUTF8BytesWithoutBOM(buffer))
                {
                    return new UTF8Encoding(false);
                }
                return System.Text.Encoding.Default;
            }
        }
        /// <summary>
        /// 判断是否不包含 bom的utf8格式
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        private static bool IsUTF8BytesWithoutBOM(byte[] data)
        {
            try
            {
                int charByteCounter = 1; //计算当前正分析的字符应还有的字节数
                byte curByte; //当前分析的字节.
                for (int i = 0; i < data.Length; i++)
                {
                    curByte = data[i];
                    if (charByteCounter == 1)
                    {
                        if (curByte >= 0x80)
                        {
                            //判断当前
                            while (((curByte <<= 1) & 0x80) != 0)
                            {
                                charByteCounter++;
                            }
                            //标记位首位若为非0 则至少以2个1开始 如:110XXXXX...........1111110X 
                            if (charByteCounter == 1 || charByteCounter > 6)
                            {
                                return false;
                            }
                        }
                    }
                    else
                    {
                        //若是UTF-8 此时第一位必须为1
                        if ((curByte & 0xC0) != 0x80)
                        {
                            return false;
                        }
                        charByteCounter--;
                    }
                }
                if (charByteCounter > 1)
                {
                    return false;
                    //throw new Exception("非预期的byte格式");
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        #endregion
posted @   newbigapple  阅读(82)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
点击右上角即可分享
微信分享提示