zoukankan      html  css  js  c++  java
  • 用FileStream加byte[]字节数组缓冲区读写文件

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication3
    {
        class Program
        {
            static void Main(string[] args)
            {
                //思路:就是现将要赋值的多媒体文件读取出来,然后希望如到你制定的位置
                string source = @"D:教程1.txt";
                string target = @"D:教程2.txt";
                CopyFile(source, target);
                Console.WriteLine("Copy Complete!");
                Console.ReadKey();
            }
            public static void CopyFile(string source,string target)
            {
                //1。我们创建一个负责读取的流 fsRead
                using(FileStream fsRead = new FileStream(source,FileMode.OpenOrCreate,FileAccess.Read))
                    //使用using语句减免代码,FileStream(src,mode,acs),节省Close() & Dispose()
                {
                    //2.创建一个负责写入的流 fsWrite
                    using(FileStream fsWrite = new FileStream(target,FileMode.OpenOrCreate,FileAccess.Write))
                    //使用using语句减免代码,FileStream(src,mode,acs), 节省Close() & Dispose(),两个using嵌套,内部先Close() & Dispose()
                    {
                        byte[] buffer = new byte[1024*1024*5];
                        //因为文件可能会比较大,所以我们在读取的时候,应该通过一个循环去读取
                        while (true)//循环去读取写入
                        {
                            //返回本次实际读取到的字节数
                            int r = fsRead.Read(buffer, 0, buffer.Length);
                            //读取
                            //如果返回一个0,也就意味着什么都没有读取到,表示读取完了
                            if (r == 0) 
                            { 
                                break;
                            }
                            fsWrite.Write(buffer,0,r);
                            //写入
                        }
                        
                    }
                }
            }
        }
    }
  • 相关阅读:
    AWS API Gateway Swagger定义
    linux常用命令---终端与目录操作
    虚拟机安装配置(VMware与CentOS安装)
    机器学习必会工具gensim
    汉语分词工具包jieba
    汉语拼音转换工具包pypinyin
    第一章 Python 基础
    removebg抠图小工具
    Django数据库表初始化缓存清除
    自定义分页(类封装高级版+函数低级版)
  • 原文地址:https://www.cnblogs.com/blacop/p/5962587.html
Copyright © 2011-2022 走看看