zoukankan      html  css  js  c++  java
  • 【.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);
            }
    
        }
  • 相关阅读:
    Redhat Enterprise Linux 磁带机质朴把持
    Lotus 认证介绍
    Delphi 与 DirectX 之 DelphiX(1): 安装测试
    关于结构体与类型转换的一点小技巧
    提取网页中的所有链接、点击第 n 个链接 回复 "刘丽" 的问题
    Delphi 与 DirectX 之 DelphiX(2): DelphiX 各单元概览
    模拟点击网页中的按钮 回复 "starcraft_ly" 的问题
    求助! 谁有 《inside delphiX》这本书?
    类型转换出现在赋值运算符左边的情况
    绘制一个钢琴键盘
  • 原文地址:https://www.cnblogs.com/caizl/p/4865780.html
Copyright © 2011-2022 走看看