C#基础(三十九)BinaryReader/Writer类、StreamReader/Writer类、filestream类、File类读写文件及其区别

参考https://blog.csdn.net/iloveyou00003/article/details/77478277

https://blog.csdn.net/ymnl_gsh/article/details/80723050

https://blog.csdn.net/Hana_one/article/details/83833479

https://blog.csdn.net/e295166319/article/details/52704080

https://bbs.csdn.net/topics/390121874(这篇博客和我遇到的问题一致)

https://blog.csdn.net/xpj8888/article/details/84999413 C# 基础(十二)C# 文本文件的读写、清零,将文本文件的内容读到字符串:主要应用在写日志

一、问题描述

        很显然,上述的几篇博客,大概讲述了Filestream的读写、BinaryReader的读写。其实,在C#中,我们常常可以用BinaryReader/Writer类、StreamReader/Writer类、filestream类、File类读写文件及其区别。

      

1.1、filestream读数据和BinaryReader读的数据,是一样的,但是BinaryReader还可以读取成字符串等等格式。这就是BinaryReader的优势。

 

        byte[] bytPictureContent = new byte[0];      //图片内容
        byte[] bytPictureContent2 = new byte[0];      //图片内容      
        /// <summary>
        /// 图片转字节
        /// </summary>
        /// <param name="Path"></param>
        /// <returns></returns>
        private byte[] ImageConvertToByte(string Path)
        {
            byte[] bytContent = new byte[0];
            try
            {
                FileStream fs = new FileStream(Path, FileMode.Open, FileAccess.Read);
                bytContent = new byte[fs.Length];
                fs.Read(bytContent, 0, Convert.ToInt32(fs.Length));
                fs.Close();
            }
            catch(Exception ex)
            {
                MessageBox.Show("SendPackageFunc()——图片转字节发生错误" + ex.ToString());
            }
            return bytContent;
        }

        
        private byte[] ImageConvertToByte2(string Path)
        {
            byte[] bytContent = new byte[0];
            try
            {
                using (BinaryReader reader = new BinaryReader(File.Open(Path, FileMode.Open)))
                {
                    //reader.Read(bytContent, 0, bytContent.Length);
                    bytContent = reader.ReadBytes(bytPictureContent.Length);
                }           
            }
            catch (Exception ex)
            {
                MessageBox.Show("SendPackageFunc()——图片转字节发生错误" + ex.ToString());
            }
            return bytContent;
        }

  

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我爱AI

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值