指定编码读取文件的内容

2014-10-24 16:06:46  访问(2177) 赞(0) 踩(0)


        /// <summary>
        /// 指定编码读取文件的内容
        /// </summary>
        /// <param name="txtFilePath"></param>
        /// <param name="en"></param>
        /// <returns></returns>
        public static string ReadFile(string txtFilePath, Encoding en)
        {
            if (txtFilePath == null || txtFilePath.Length == 0 || txtFilePath.Trim().Length == 0)
                throw new Exception("txtFilePath为null或为空。");

            if (!File.Exists(txtFilePath))
                throw new Exception(txtFilePath + " 文件不存在。");

            string theResult = null;

            StreamReader fileStream = null;

            try
            {

                fileStream = new StreamReader(txtFilePath, en);

                theResult = fileStream.ReadToEnd();

            }
            catch (Exception err)
            {
                throw err;
            }
            finally
            {
                if (fileStream != null)
                {
                    fileStream.Close();
                }
            }

            return theResult;
             
        }


标签:读取文件内容    指定编码 

上一条:

下一条:


 

相关评论

评论加载中……
 

发表评论

类型:
内容:
  (Alt+Enter)