【.Net】Byte,Stream,File的转换

引言
      文件的传输和读写通常都离不开Byte,Stream,File这个类,这里我简单封装一下,方便使用.

帮助类

   

    public static class FileHelper
    {

        /// <summary>
        ///Stream转化成byte数组
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public static byte[] StreamToBytes(Stream stream)
        {
            MemoryStream  memoryStream =new MemoryStream();
            stream.CopyTo(memoryStream);
            return memoryStream.ToArray();

           // MemoryStream memoryStream = new MemoryStream();
            //byte[] btContent = new byte[512];
            //int intSize = 0;
            //intSize = stream.Read(btContent, 0, 512);
            //while (intSize > 0)
            //{
            //    memoryStream.Write(btContent, 0, intSize);
            //    intSize = stream.Read(btContent, 0, 512);
            //}
            // return memoryStream.ToArray();
        }

        /// <summary>
        ///byte数组转化成Stream
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static Stream BytesToStream(byte[] bytes)
        {
            return new MemoryStream(bytes);
        }

        /// <summary>
        ///将流保存成文件
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="filepath">保存文件的路径</param>
        public static void StreamToFile(Stream stream,string filepath)
        {       
                byte[] bytes = StreamToBytes(stream);
                FileStream fileStream = new FileStream(filepath, FileMode.Create);
                fileStream.Write(bytes, 0, bytes.Length);
                fileStream.Flush();
                fileStream.Close();
        }

        /// <summary>
        ///将数组保存成文件
        /// </summary>
        /// <param name="bytes">byte数组</param>
        /// <param name="filepath">保存文件的路径</param>
        public static void BytesToFile(byte[] bytes, string filepath)
        {
                //FileStream fileStream = new FileStream(filepath, FileMode.Create);
                //fileStream.Write(bytes, 0, bytes.Length);
                //fileStream.Flush();   
                //fileStream.Close();

                File.WriteAllBytes(filepath, bytes);

        }

        /// <summary>
        ///读取文件转化成流
        /// </summary>
        /// <param name="filepath">文件路径</param>
        /// <returns></returns>
        public static Stream FileToStream(string filepath)
        {
            FileStream fileStream = new FileStream(filepath, FileMode.Open);
            MemoryStream memoryStream = new MemoryStream();
            fileStream.CopyTo(memoryStream);
            fileStream.Close();
            memoryStream.Position = 0;
            return memoryStream;
        }

        /// <summary>
        ///读取文件转成问Byte数组
        /// </summary>
        /// <param name="filepath"></param>
        /// <returns></returns>
        public static byte[] FileToBytes(string filepath)
        {
            //FileStream fileStream = new FileStream(filepath, FileMode.Open);
            //byte[] bytes= StreamToBytes(fileStream);
            //fileStream.Close();
            //return bytes;

            return  File.ReadAllBytes(filepath);
        }


        /// <summary>
        /// 读取文件转化成字符串
        /// </summary>
        /// <param name="filepath">文件路径</param>
        /// <param name="encode"></param>
        /// <returns>字符串</returns>
        public static string FileToString(string filepath, Encoding encode)
        {
            //StreamReader streamReader = new StreamReader(filepath, encode);
            //string text = streamReader.ReadToEnd();
            //streamReader.Close();

            string text = File.ReadAllText(filepath, encode);
            return text;
        }

        /// <summary>
        /// 流转化成字符串
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="encode"></param>
        /// <returns>字符串</returns>
        public static string StreamToString(Stream stream, Encoding encode)
        {
            if (stream.CanSeek)
            {
                stream.Position = 0;
            }
            StreamReader streamReader = new StreamReader(stream,encode);
            string text =streamReader.ReadToEnd();
            streamReader.Close();
            return text;
        }

        /// <summary>
        /// byte数组转化字符串
        /// </summary>
        /// <param name="bytes">byte数组</param>
        /// <param name="encode">编码</param>
        /// <returns></returns>
        public static string BytesToString(byte[] bytes, Encoding encode)
        {
            return encode.GetString(bytes);
        }

        /// <summary>
        /// 字符串转化byte数组
        /// </summary>
        /// <param name="text">字符串</param>
        /// <param name="encode">编码</param>
        /// <returns></returns>
        public static byte[] StringToBytes(string text, Encoding encode)
        {
            return encode.GetBytes(text);
        }

        /// <summary>
        /// 字符串转化成流
        /// </summary>
        /// <param name="text">字符串</param>
        /// <param name="encode">编码</param>
        /// <returns></returns>
        public static Stream StringToStream(string text, Encoding encode)
        {
           var bytes= StringToBytes(text, encode);
           return BytesToStream(bytes);
        }

        /// <summary>
        /// 往文件中追加文本内容
        /// </summary>
        /// <param name="text">文本内容</param>
        /// <param name="filepath">文件路径</param>
        /// <param name="encode">编码</param>
        public static void AppendStringToFile(string text, string filepath, Encoding encode)
        {
            File.AppendAllText(filepath, Environment.NewLine + text, encode);
        }

    }

 

posted @ 2015-10-10 09:18  Caizl  阅读(1304)  评论(0编辑  收藏  举报