C#文件流(FileStream)的学习

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _08文件流1
{
    class Program
    {
        static void Main(string[] args)
        {
            //文件流的使用方式
            //将字符串转换为字节数组
            string msg = "测试字符串";
            byte[] bytes = Encoding.UTF8.GetBytes(msg);
            //将Byte数组转换为字符串
            string newmsg = Encoding.UTF8.GetString(bytes);
            Console.WriteLine(newmsg);

            //流操作的都是字节
            //通过文件流写文件
            #region 通过文件流写文件 
            //文件流使用步骤
            //1.创建文件流
            FileStream fswirte = new FileStream("wirte.txt", FileMode.OpenOrCreate, FileAccess.Write);

            //2.执行读写操作
            string strsource = "今天下雨啦";
            byte[] bytesource = Encoding.UTF8.GetBytes(strsource);
            //参数1:将自定字节数组写入到文件
            //参数2:字节数组偏移量
            //参数3:写入文件字节的个数
            fswirte.Write(bytesource, 0, bytesource.Length);

            //3.清空缓冲区,关闭文件流,释放资源
            fswirte.Flush(); //文件流内部有缓冲,并没有直接写入磁盘中
            fswirte.Close();
            fswirte.Dispose();
            Console.WriteLine("=> Write => OK");
            #endregion


            #region 通过文件流读取文件 
            FileStream fsread = new FileStream("wirte.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite);
            //根据文件的总字节数,来创建一个数组,这种方式会将文件内容一次性读取到字节数组中
            byte[] byteread = new byte[fsread.Length];
            //将读取的字节数组读取到字节数组中
            fsread.Read(byteread, 0, byteread.Length);
            string targetpath = @"D:\read.txt";

            FileStream fswirte2 = new FileStream(targetpath, FileMode.OpenOrCreate, FileAccess.Write);
            fswirte2.Write(byteread, 0, byteread.Length);

            fsread.Flush();
            fsread.Close();
            fsread.Dispose();

            fswirte2.Flush();
            fswirte2.Close();
            fswirte2.Dispose();

            Console.WriteLine(" => Read => OK");
            #endregion
            Console.ReadKey();

        }



  //大文件拷贝    

 private static void BigFileCopy(string sourcePath, string targetPath)

        {
            //1.创建一个读取文件的文件流
            using (FileStream fsread = new FileStream(sourcePath, FileMode.OpenOrCreate, FileAccess.ReadWrite))
            {
                //2.创建一个写入文件的文件流
                using (FileStream fswirte = new FileStream(targetPath, FileMode.OpenOrCreate, FileAccess.ReadWrite))
                {
                    //设置每次读写文件是缓冲区大小为1M
                    byte[] bytes = new byte[1024 * 1024 * 5];
                    //返回实际读取到的字节个数
                    int length = 0;
                    int count = 0;
                    while ((length = fsread.Read(bytes, 0, bytes.Length)) > 0)
                    {
                        //将读取到的字节写入到文件中
                        //参数3 =》应为实际读取到的字节个数,而不是字节数组的长度
                        fswirte.Write(bytes, 0, length);
                        Console.WriteLine(count++);
                    }
                }
            }
        }    }
}


        /// <summary>
        /// 下载方法
        /// </summary>
        /// <param name="FileName">文件名称</param>
        /// <param name="FilePath">文件路径</param>
        /// <param name="Response"></param>
        public static void FileDownload(string FileName, string FilePath, HttpResponse Response)
        {

            #region  流方式下载
            //string fileName = "aaa.txt";//客户端保存的文件名
            //string filePath = Server.MapPath("DownLoad/aaa.txt");//路径

            if (!string.IsNullOrEmpty(FilePath))
            {
                if (File.Exists(FilePath))
                {
                    //以字符流的形式下载文件
                    FileStream fs = new FileStream(FilePath, FileMode.Open); // FileStream文件读写
                    byte[] bytes = new byte[(int)fs.Length];
                    fs.Read(bytes, 0, bytes.Length);
                    fs.Close();
                    Response.ContentType = "application/octet-stream";
                    //通知浏览器下载文件而不是打开
                    Response.AddHeader("Content-Disposition", "attachment;  filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8));
                    Response.BinaryWrite(bytes);
                    Response.Flush();
                    Response.End();
                }

            }
       
        }

        public static void DownloadFile(string fileName, byte[] bytes)
        {
            HttpContext.Current.Response.ContentType = "application/octet-stream";
            //通知浏览器下载文件而不是打开
            HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;  filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
            HttpContext.Current.Response.BinaryWrite(bytes);
            HttpContext.Current.Response.Flush();
            HttpContext.Current.Response.End();
        }

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值