当前位置: 移动技术网 > IT编程>开发语言>.net > stream,file,filestream,memorystream简单的整理

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

2019年01月29日  | 移动技术网IT编程  | 我要评论

阮惠健,官运txt下载,雨之牙

一、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;

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网