zoukankan      html  css  js  c++  java
  • 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<Studentstu中  

      /// <summary>
      /// 读取信息方法
      /// </summary>
      /// <returns>读取是否成功</returns>
      public void ReadInfo(List<Studentstu)
      {
       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 0; 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<Studentstu中的数据


      /// <summary>
      /// 写入信息方法
      /// </summary>
      /// <returns>写入是否成功</returns>
      public void WriteInfo(List<Studentstu)
      {
       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("保存成功!");
      }
  • 相关阅读:
    文件上传跨域解决方案-jQuery-File-Upload
    求数列的的增幅,已知起始列和结束列,中间阶梯数
    mud那些坑 find_object问题
    jquery mCustomScrollbar使用
    JS的Date对象
    JS获取当前日期
    Go数组求和
    Go九九乘法表
    Go语言基础之time包
    Go语言基础之流程控制
  • 原文地址:https://www.cnblogs.com/moss_tan_jun/p/1950520.html
Copyright © 2011-2022 走看看