• C#FileStream读写大文件!代码可用!


     1 using System;
     2 using System.Collections.Generic;
     3 using System.IO;
     4 using System.Reflection;
     5 using System.Text;
     6 
     7 namespace IO目录管理
     8 {
     9     class Program
    10     {
    11         private string _StrSourcePath = @"C:UsersMODesktop1.txt"; //源文件目录 
    12         private string _StrTagrgetPath = @"C:UsersMODesktop2.txt"; //目标文件目录
    13         public void Test()
    14         {
    15             //路径合法性判断
    16             if (File.Exists(_StrSourcePath))
    17             {
    18                 //构造读取文件流对象
    19                 using (FileStream fsRead = new FileStream(_StrSourcePath, FileMode.Open)) //打开文件,不能创建新的
    20                 {
    21                     //构建写文件流对象
    22                     using (FileStream fsWrite = new FileStream(_StrTagrgetPath, FileMode.Create)) //没有找到就创建
    23                     {
    24                         //开辟临时缓存内存
    25                         byte[] byteArrayRead = new byte[1024 * 1024]; // 1字节*1024 = 1k 1k*1024 = 1M内存
    26 
    27                         //通过死缓存去读文本中的内容
    28                         while (true)
    29                         {
    30                             //readCount 这个是保存真正读取到的字节数
    31                             int readCount = fsRead.Read(byteArrayRead, 0, byteArrayRead.Length);
    32 
    33                             //开始写入读取到缓存内存中的数据到目标文本文件中
    34                             fsWrite.Write(byteArrayRead, 0, readCount);
    35 
    36 
    37                             //既然是死循环 那么什么时候我们停止读取文本内容 我们知道文本最后一行的大小肯定是小于缓存内存大小的
    38                             if (readCount < byteArrayRead.Length)
    39                             {
    40                                 break; //结束循环
    41                             }
    42                         }
    43                     }
    44                 }
    45             }
    46             else
    47             {
    48                 Console.WriteLine("源路径或者目标路径不存在。");
    49             }
    50         }
    51 
    52         static void Main(string[] args)
    53         {
    54             Program p = new Program();
    55             p.Test();
    56             Console.ReadKey();
    57 
    58         }
    59     }
    60 }
  • 相关阅读:
    vue后台模板推荐
    Webstorm的一些常用快捷键
    dataTables 插件学习整理
    js阻止事件冒泡
    vscode常用快捷键
    VScode插件以及配置
    今日笔记2
    ES6中的import()函数
    ES6之class 中 constructor 方法 和 super 的作用
    JS设计模式一:单例模式
  • 原文地址:https://www.cnblogs.com/momj/p/14397863.html
Copyright © 2020-2023  润新知