C#读写二进制文件

本文要介绍的C#本地读写二进制文件,二进制文件指保存在物理磁盘的一个文件。

第一步:读写文件转成流对象。其实就是读写文件流 (FileStream对象,在System.IO命名空间中)。File、FileInfo、FileStream这三个类可以将打开文件,并变成文件 流。下面是引用微软对File、FileInfo、FileStream的介绍
System.IO.File类 提供用于创建、复制、删除、移动和打开文件的静态方法,并协助创建 FileStream 对象。
System.IO.FileInfo类 提供创建、复制、删除、移动和打开文件的实例方法,并且帮助创建 FileStream 对象。无法继承此类。
System.IO.FileStream类 公开以文件为主的 Stream,既支持同步读写操作,也支持异步读写操作。
我直接使用 FileStream,他继承于Stream

第二步:读写流。读写二进制文件用System.IO.BinaryReaderSystem.IO.BinaryWriter类;读写文本文件用System.IO.TextReaderSystem.IO.TextWriter类。下面是我的实体 (即要保持到文件的数据)
  ///   <summary>
  ///  学生基本信息类
  ///   </summary>
  public   class   Student
 {
   ///   <summary>
   ///  学号变量
   ///   </summary>
   private   String  _id;
   ///   <summary>
   ///  姓名变量
   ///   </summary>
   private   String  _name;
   ///   <summary>
   ///  语文成绩变量
   ///   </summary>
   private   Double  _score1;
   ///   <summary>
   ///  数学成绩变量
   ///   </summary>
   private   Double  _score2;
   ///   <summary>
   ///  英语成绩变量
   ///   </summary>
   private   Double  _score3;


   ///   <summary>
   ///  学号属性
   ///   </summary>
   public   String  Id
  {
    get  {  return  _id; }
    set  { _id =  value ; }
  }
   ///   <summary>
   ///  姓名属性
   ///   </summary>
   public   String  Name
  {
    get  {  return  _name; }
    set  { _name =  value ; }
  }
   ///   <summary>
   ///  语文成绩属性
   ///   </summary>
   public   Double  Score1
  {
    get  {  return  _score1; }
    set  { _score1 =  value ; }
  }
   ///   <summary>
   ///  数学成绩属性
   ///   </summary>
   public   Double  Score2
  {
    get  {  return  _score2; }
    set  { _score2 =  value ; }
  }
   ///   <summary>
   ///  英语成绩属性
   ///   </summary>
   public   Double  Score3
  {
    get  {  return  _score3; }
    set  { _score3 =  value ; }
  }
 }

 下面是我的读方法,读取文件中的信息到参数List<Student> stu中  

  ///   <summary>
   ///  读取信息方法
   ///   </summary>
   ///   <returns> 读取是否成功 </returns>
   public   void  ReadInfo( List < Student > stu)
  {
    Console .WriteLine( "请输入文件读取路径:(键入回车为默认路径)" );
    String  filename =  Console .ReadLine();
    FileStream  fs;
    //默认路径
    if  (filename ==  "" )
   {
    fs =  new   FileStream ( "student.dll" FileMode .Open);
   }
    else
   {
     //如果文件不存在,就提示错误
     if  (! File .Exists(filename))
    {
      Console .WriteLine( "\n\t读取失败!\n错误原因:可能不存在此文件" );
      return ;
    }
     //否则创建文件
    fs =  new   FileStream (filename,  FileMode .Open);
   }
    //使用二进制读取
    BinaryReader  br =  new   BinaryReader (fs);
    Console .Write( "读取信息将覆盖现有的信息,继续吗?y/n :" );
    String  command =  Console .ReadLine();
    if  (command ==  "y"  || command ==  "Y" )
   {
     for  ( int  i = 0; i < stu.Count; i++)
    {
     stu.RemoveAt(i);
    }
     //从磁盘上读取信息
     try
    {
      while  ( true )
     {
       Student  student =  new   Student ();
      student.Id = br.ReadString();
      student.Name = br.ReadString();
      student.Score1 = br.ReadDouble();
      student.Score2 = br.ReadDouble();
      student.Score3 = br.ReadDouble();
      stu.Add(student);
      student =  null ;
     }
    }
     catch  ( Exception )
    {
      Console .WriteLine( "\n\n读取结束!" );
    }
   }
   br.Close();
   fs.Close();
  }


下面是我的写入方法,写入参数 List < Student > stu中的数据


   ///   <summary>
   ///  写入信息方法
   ///   </summary>
   ///   <returns> 写入是否成功 </returns>
   public   void  WriteInfo( List < Student > stu)
  {
    Console .WriteLine( "请输入文件保存路径:(键入回车为默认路径)" );
    FileStream  fs;
    String  filename =  Console .ReadLine();
    //默认路径
    if  (filename ==  "" )
   {
    fs =  new   FileStream ( "student.dll" FileMode .Create);
   }
    //手动输入路径
    else
   {
     //如果文件存在,就提示错误
     if  ( File .Exists(filename))
    {
      Console .WriteLine( "\n\t保存失败!\n错误原因:可能存在相同文件" );
      return ;
    }
     //否则创建文件
    fs =  new   FileStream (filename,  FileMode .Create);
   }
    //数据保存到磁盘中
    BinaryWriter  bw =  new   BinaryWriter (fs);
    foreach  ( Student  student  in  stu)
   {
    bw.Write(( String )student.Id);
    bw.Write(( String )student.Name);
    bw.Write(( Double )student.Score1);
    bw.Write(( Double )student.Score2);
    bw.Write(( Double )student.Score3);
    bw.Flush();
   }
   bw.Close();
   fs.Close();
    Console .WriteLine( "保存成功!" );
  }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值