码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2015-10-10 10:38:28      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:

引言
      文件的传输和读写通常都离不开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);
        }

    }

 

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

标签:

原文地址:http://www.cnblogs.com/caizl/p/4865780.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!