国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > C# > 正文

使用C#讀物二進(jìn)制文件的方法(源代碼)

2023-05-15 12:29:34
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文要介紹的C#本地讀寫二進(jìn)制文件,二進(jìn)制文件指保存在物理磁盤的一個(gè)文件。

第一步:讀寫文件轉(zhuǎn)成流對(duì)象。其實(shí)就是讀寫文件流 (FileStream對(duì)象,在System.IO命名空間中)。File、FileInfo、FileStream這三個(gè)類可以將打開文件,并變成文件 流。下面是引用微軟對(duì)File、FileInfo、FileStream的介紹

System.IO.File類 提供用于創(chuàng)建、復(fù)制、刪除、移動(dòng)和打開文件的靜態(tài)方法,并協(xié)助創(chuàng)建 FileStream 對(duì)象。

System.IO.FileInfo類 提供創(chuàng)建、復(fù)制、刪除、移動(dòng)和打開文件的實(shí)例方法,并且?guī)椭鷦?chuàng)建 FileStream 對(duì)象。無(wú)法繼承此類。

System.IO.FileStream類 公開以文件為主的 Stream,既支持同步讀寫操作,也支持異步讀寫操作。
我直接使用 FileStream,他繼承于Stream

第二步:讀寫流。讀寫二進(jìn)制文件用System.IO.BinaryReaderSystem.IO.BinaryWriter類;讀寫文本文件用System.IO.TextReaderSystem.IO.TextWriter類。下面是我的實(shí)體 (即要保持到文件的數(shù)據(jù))
  /// <summary>
 /// 學(xué)生基本信息類
 /// </summary>
 public class Student
 {
  /// <summary>
  /// 學(xué)號(hào)變量
  /// </summary>
  private String _id;
  /// <summary>
  /// 姓名變量
  /// </summary>
  private String _name;
  /// <summary>
  /// 語(yǔ)文成績(jī)變量
  /// </summary>
  private Double _score1;
  /// <summary>
  /// 數(shù)學(xué)成績(jī)變量
  /// </summary>
  private Double _score2;
  /// <summary>
  /// 英語(yǔ)成績(jī)變量
  /// </summary>
  private Double _score3;


  /// <summary>
  /// 學(xué)號(hào)屬性
  /// </summary>
  public String Id
  {
   get return _id; }
   set _id value}
  }
  /// <summary>
  /// 姓名屬性
  /// </summary>
  public String Name
  {
   get return _name; }
   set _name value}
  }
  /// <summary>
  /// 語(yǔ)文成績(jī)屬性
  /// </summary>
  public Double Score1
  {
   get return _score1; }
   set _score1 value}
  }
  /// <summary>
  /// 數(shù)學(xué)成績(jī)屬性
  /// </summary>
  public Double Score2
  {
   get return _score2; }
   set _score2 value}
  }
  /// <summary>
  /// 英語(yǔ)成績(jī)屬性
  /// </summary>
  public Double Score3
  {
   get return _score3; }
   set _score3 value}
  }
 }

 下面是我的讀方法,讀取文件中的信息到參數(shù)List<Studentstu中  

  /// <summary>
  /// 讀取信息方法
  /// </summary>
  /// <returns>讀取是否成功</returns>
  public void ReadInfo(List<Studentstu)
  {
   Console.WriteLine("請(qǐng)輸入文件讀取路徑:(鍵入回車為默認(rèn)路徑)");
   String filename Console.ReadLine();
   FileStream fs;
   //默認(rèn)路徑
   if (filename == "")
   {
    fs new FileStream("student.dll"FileMode.Open);
   }
   else
   {
    //如果文件不存在,就提示錯(cuò)誤
    if (!File.Exists(filename))
    {
     Console.WriteLine("/n/t讀取失敗!/n錯(cuò)誤原因:可能不存在此文件");
     return;
    }
    //否則創(chuàng)建文件
    fs new FileStream(filename, FileMode.Open);
   }
   //使用二進(jìn)制讀取
   BinaryReader br new BinaryReader(fs);
   Console.Write("讀取信息將覆蓋現(xiàn)有的信息,繼續(xù)嗎?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讀取結(jié)束!");
    }
   }
   br.Close();
   fs.Close();
  }


下面是我的寫入方法,寫入?yún)?shù)List<Studentstu中的數(shù)據(jù)


  /// <summary>
  /// 寫入信息方法
  /// </summary>
  /// <returns>寫入是否成功</returns>
  public void WriteInfo(List<Studentstu)
  {
   Console.WriteLine("請(qǐng)輸入文件保存路徑:(鍵入回車為默認(rèn)路徑)");
   FileStream fs;
   String filename Console.ReadLine();
   //默認(rèn)路徑
   if (filename == "")
   {
    fs new FileStream("student.dll"FileMode.Create);
   }
   //手動(dòng)輸入路徑
   else
   {
    //如果文件存在,就提示錯(cuò)誤
    if (File.Exists(filename))
    {
     Console.WriteLine("/n/t保存失??!/n錯(cuò)誤原因:可能存在相同文件");
     return;
    }
    //否則創(chuàng)建文件
    fs new FileStream(filename, FileMode.Create);
   }
   //數(shù)據(jù)保存到磁盤中
   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("保存成功!");
  }
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 仙居县| 青冈县| 荃湾区| 广灵县| 北辰区| 罗江县| 翼城县| 新津县| 山阴县| 漠河县| 高青县| 平顶山市| 图们市| 桂阳县| 永宁县| 普洱| 贵定县| 钦州市| 襄樊市| 德化县| 台北县| 黄冈市| 股票| 九寨沟县| 蚌埠市| 连州市| 绥中县| 彭泽县| 驻马店市| 西吉县| 榆林市| 肃南| 沙雅县| 聂拉木县| 根河市| 额尔古纳市| 水富县| 洪江市| 乌鲁木齐市| 许昌县| 响水县|