乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      C#讀寫二進(jìn)制文件

       junshuai103 2015-07-22
      本文要介紹的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ì)象。無法繼承此類。
      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>
        /// 語文成績(jī)變量
        /// </summary>
        private Double _score1;
        /// <summary>
        /// 數(shù)學(xué)成績(jī)變量
        /// </summary>
        private Double _score2;
        /// <summary>
        /// 英語成績(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>
        /// 語文成績(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>
        /// 英語成績(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("保存成功!");
        }

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶 評(píng)論公約

        類似文章 更多