stream,file,filestream,memorystream简单的整理

一、Stream
  什么是stream?(https://www.cnblogs.com/JimmyZheng/archive/2012/03/17/2402814.html#no8)

  定义:提供字节序列的一般视图。

  什么是字节序列?
  定义:字节按照一定的顺序进行排序组成了字节序列,字节对象都被存储为连续的字节序列。
  一个字节由8个二进制组成。

  stream类的结构,属性和方法
  构造函数:
  stream类由一个protected类型的构造函数,但是它是个抽象类,无法直接如下使用
  Stream stream = new Stream();
  所以我们自定义一个流继承自Stream看看那些属性必须重写或者自定义:

  1.CanRead:只读属性,判断该流是否能够读取;
  2.CanSeek:只读属性,判断该流是否支持跟踪查找;
  3.CanWrite:只读属性,判断当前流是否可写;
  4.Void Flush():当我们使用流写文件时,数据流会先进入到缓冲区中,而不会立刻写入文件,当执行这个方法后,缓冲区的数据流会立即注入基础流。(使用此方法可以将所有信息从基础缓冲区移动到其目标或清除缓冲区,或者同时执行这种操作。
  当使用StreamWrite或者BinaryWrite类时,不要刷新Stream基对象而应该使用该类的Flush或Close方法,此方法确保首先将该数据刷新至基础流,然后再将其写入文件。
  5.Length:表示流的长度
  6.Position属性:虽然从字面中可以看出这个Position属性只是标示了流中的一个位置,当Stream对象被缓存了,会导致Position属性在流中无法找到正确的位置,解决方案:用Using语句将流对象包裹起来,用完之后关闭回收即可。(using()语句会自动回收)


二、File
  提供用于创建、复制、删除、移动和打开单一文件的静态方法,并协助创建 FileStream 对象。
  继承 Object->File
  方法:
  AppendAllLines(String Filepath, IEnumerable<String> content) FilePath: 需要追加内容的文件 content:需要追加的行File.AppendAllText(String filepath,String content,Encoding) 将指定的字符串追加到文件中,如果文件还不存在则创建该文件encoding 字符编码
  public static System.IO.StreamWriter AppendText (string path);返回StreamWriter一个流写入器,它将 UTF-8 编码文本追加到指定文件或新文件。 创建一个StreamWriter,它将 UTF-8 编码文本追加到现有文件或新文件(如果指定文件不存在)
  public static void Copy (string sourceFileName, string destFileName);sourceFileName 要复制的文件。destFileName 目标文件的名称,它不能是一个目录或现有文件.
  public static System.IO.FileStream Create (string path);//path要创建的文件的路径及名称。返回一个FileStream,它提供对 path 中指定的文件的读/写访问。

eg.  

using (FileStream fs = File.Create(path))
  {
    Byte[] info = new UTF8Encoding(true).GetBytes("This is some text in the file.");
    // Add some information to the file.
    fs.Write(info, 0, info.Length);
  }

  public static System.IO.FileStream Create (string path, int bufferSize);//path要创建的文件的路径及名称 bufferSize Int32用于读取和写入到文件的已放入缓冲区的字节数。返回FileStream一个具有指定缓冲区大小的 FileStream,它提供对 path 中指定的文件的读/写访问。
  public static System.IO.FileStream OpenRead (string path);//打开文件进行读取,返回FileStream指定路径上的只读FileStream。
  public static void Move (string sourceFileName, string destFileName);//文件移动,从sourceFileName移动到destFileName


三、FileStream
  为文件提供stream,既支持同步读写又支持异步读写。是对文件的操作,继承自Stream。
  方法:
  public FileStream(string path, FileMode mode, FileAccess access);
  path指明文件所在的路径信息;
  mode是FileMode的枚举值,表示文件打开或创建的方式
  CreateNew:指定操作系统应创建新文件,如果文件已经存在,则引发IOException;
  Create:指定操作系统应创建新文件,如果文件已经存在,它将被覆盖;
  Open:指定操作系统应打开现有文件,如果文件不存在,则引发FileNotFoundException;
  OpenOrCreate:指定操作系统应打开文件,如果文件不存在,则创建新文件;
  Truncate:指定操作系统应打开现有文件,文件一旦打开,就将截断为零字节大小;
  Append:打开先有文件并把Position设置至文件尾,如果文件不存在将创建新文件。Append只能同FileAccess.Write一起使用

  access是FileAccess的枚举值,它控制对文件的访问权限
  Read:打开文件用于只读;
  Write:打开文件用于只写;
  ReadWrite:打开文件,用于读写;
  //创建新文件
  FileStream fileStream = new FileStream(@"d:\test.txt", FileMode.Create);
  //读取
  FileStream fileStream= new FileStream(@"d:\test.txt", FileMode.Open);
  FileStream fs=File.OpenRead(@"c:\file.txt"); //这样是返回只读文件流。
四、MemoryStream
  和文件流不同,MemoryStream类表示的是保存在内存中的数据流,由内存流封装的数据可以在内存中直接访问。内存一般用于暂时缓存数据以降低应用程序对临时缓冲区和临时文件的需要。
  相关用法:

  MemoryStream ms = new MemoryStream();
  byte[] buffer = new byte[stream.Length];
  stream.Position = 0;
  int arrbyte = stream.Read(buffer, 0, buffer.Length);
  ms.Write(buffer, 0, arrbyte);

  FileStream fs = new FileStream(HttpContext.Current.Server.MapPath(FilePath), FileMode.Create);

  //写入流文件
  ms.WriteTo(fs);

  //清空内存
  ms.Close();
  fs.Close();
  fs = null;
  ms = null;

转载于:https://www.cnblogs.com/The-Invisible/p/10330156.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C#中的Stream是一个抽象类,它是处理输入和输出的基类。Stream类提供了一组操作,可以让我们轻松地读取和写入数据流。 以下是C#中使用Stream的基本方法: 1. 创建一个Stream对象:可以使用FileStreamMemoryStream、NetworkStream等类来创建Stream对象。 2. 写入数据:使用Write方法可以将数据写入到Stream中。 3. 读取数据:使用Read方法可以从Stream中读取数据。 4. 关闭Stream:使用Close方法可以关闭Stream对象。 下面是一个简单的示例代码,演示了如何使用FileStream类来读写文件: ``` using System; using System.IO; class Program { static void Main(string[] args) { string fileName = "data.txt"; // 将一些数据写入文件 using (FileStream stream = new FileStream(fileName, FileMode.Create)) { byte[] buffer = new byte[] { 1, 2, 3, 4, 5 }; stream.Write(buffer, 0, buffer.Length); } // 读取文件中的数据 using (FileStream stream = new FileStream(fileName, FileMode.Open)) { byte[] buffer = new byte[stream.Length]; stream.Read(buffer, 0, buffer.Length); foreach (byte b in buffer) { Console.WriteLine(b); } } // 关闭文件 File.Delete(fileName); } } ``` 在上面的代码中,我们首先使用FileStream类创建了一个名为“data.txt”的文件,并将一些数据写入到文件中。然后,我们再次使用FileStream类打开文件,并读取其中的数据。最后,我们使用File.Delete方法删除文件。 这只是一个简单的示例,Stream类还提供了许多其他的方法和属性,可以满足不同的需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值