如何使用C# FileStream类?

来源:这里教程网 时间:2026-02-21 16:30:19 作者:

如何使用c# filestream类?

FileStream类提供了用于文件操作(例如读取和写入)的流。

创建一个像这样的对象

FileStream fstream = new FileStream("d:\ew.txt", FileMode.OpenOrCreate);

上面我们使用了 FileMode.OpenOrCreate 来打开或创建文件(如果文件尚不存在)。

以下示例展示了如何在 C# 中使用 FileStream 类 -

using System;
using System.IO;
public class Demo {
   public static void Main(string[] args) {
      FileStream fstream = new FileStream("d:\ew.txt", FileMode.OpenOrCreate);
      // write into the file
      fstream.WriteByte(90);
      // close the file
      fstream.Close();
   }
}

相关推荐