【小白专用】c#之FileStream对象读写大文件

提及文件流,不得不先说以下几个类
FileStream,MemoryStream,NetWorkStream,StreamReader,StreamWriter,TextReader,TextWriter
在用这些类之前,我们先来了解一下这些类的用途以及区别。

C# 中常用的文件流(FileStream、StreamReader/Writer、MemoryStream)_c# filestream和streamwriter-CSDN博客

读取数据:

FileStream fs = new FileStream(path,FileMode.Open);    //初始化文件流
byte[] arr = new byte[fs.Length];                      //初始化字节数组
fs.Read(arr, 0, arr.Length);                           //从流中数据读取到字节数组中
fs.Close();                                            //关闭流
string str = Encoding.UTF8.GetString(arr);             //将字节数组转换为字符串
Console.WriteLine(str);

写入数据

FileStream fs = new FileStream(path,FileMode.Append);  //初始化文件流
byte[] arr = Encoding.UTF8.GetBytes("程序人生道可道");   //将字符串转换为字节数组
fs.Write(arr,0,arr.Length);                            //将字节数组写入文件流
fs.Close(); 

二:StreamReader/StreamWriter类

用途:主要用来处理流数据,它们分别提供了高效的流读取/写入功能。
优点:可以直接用字符串进行读写,而不用转换成字节数组。
注意:对于文本文件的读写,通常用 StreamReader 类和 StreamWriter 类更方便,其底层是通过FileStream实现读写文本文件。

读取数据:
 

FileStream fs = new FileStream(path,FileMode.Open);  //初始化文件流
StreamReader sr = new StreamReader(fs);              //初始化StreamReader
string line = sr.ReadLine();                         //直接读取一行
string line = sr.ReadToEnd()                         //读取全文
sr.Close();                                          //关闭流
fs.Close();                                          //关闭流
Console.WriteLine(line);

经试验:读取数据时 sr 和 fs 关闭的顺序颠倒同样可以读取到数据,考虑代码规范,常规写法就行。

写入数据:

FileStream fs = new FileStream(path,FileMode.Append); //初始化文件流
StreamWriter sw = new StreamWriter(fs);               //初始化StreamWriter
sw.WriteLine("程序人生道可道");                         //写入一行数据
sw.Close();                                           //关闭流
fs.Close();                                           //关闭流

经试验:写入数据时 fs 一定要在 sw 后面关闭,否则会抛出异常(因为你在写入数据之前,你已经把文件流给关闭了,肯定写不进去数据了)

“` 
using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Reflection; 
using System.Text;

namespace IO目录管理 
{ 
class Program 
{ 
private string _StrSourcePath = @”E:\TestDir\Test\1.txt”; //源文件目录 
private string _StrTagrgetPath = @”F:\TestDir\Test\1.txt”; //目标文件目录

 public void Test()
  {
    //路径合法性判断
    if(File.Exists(_StrSourcePath))
    {
      //构造读取文件流对象
      using (FileStream fsRead = new FileStream(_StrSourcePath, FileMode.Open)) //打开文件,不能创建新的
      {
        //构建写文件流对象
        using (FileStream fsWrite = new FileStream(_StrTagrgetPath,FileMode.Create)) //没有找到就创建
        {
          //开辟临时缓存内存
          byte[] byteArrayRead = new byte[1024 * 1024]; // 1字节*1024 = 1k 1k*1024 = 1M内存

          //通过死缓存去读文本中的内容
          while(true)
          {
            //readCount 这个是保存真正读取到的字节数
            int readCount = fsRead.Read(byteArrayRead, 0, byteArrayRead.Length);

            //开始写入读取到缓存内存中的数据到目标文本文件中
            fsWrite.Write(byteArrayRead, 0, readCount);


            //既然是死循环 那么什么时候我们停止读取文本内容 我们知道文本最后一行的大小肯定是小于缓存内存大小的
            if(readCount < byteArrayRead.Length)
            {
              break; //结束循环
            }
          }
        }
      }
    }
    else
    {
      Console.WriteLine("源路径或者目标路径不存在。");
    }
  }


  static void Main(string[] args)
  {
    Program p = new Program();
    p.Test();

  }
}

}

  • 5
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值