注銷

          注銷

            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            112 隨筆 :: 7 文章 :: 18 評論 :: 0 Trackbacks

          ?

          1 .數值應保存在二進制文件

          首先列舉文本、二進制文件的操作(讀寫)方法:

          方式 1

          // 文本文件操作:創(chuàng)建 / 讀取 / 拷貝 / 刪除
          using System;
          using System.IO;
          class Test
          {
          ???string path = @"f:\t.txt";
          ???publicstaticvoid Main()
          ???{ ??????
          ??????//
          創(chuàng)建并寫入 ( 將覆蓋已有文件 )

          ????? if (!File.Exists(path))
          ??????{

          //StreamWriter m=new ??????????

          //StreamWriter(path,true,Encoding.Default,1);//ASCII,1?? Encoding.Default :即//UTF-8編碼? 這樣就可以指定編碼方式

          ????using (StreamWriter sw = File.CreateText(path))
          ?????????{
          ????????????sw.WriteLine("Hello");
          ?????????}
          ??????}
          ??????//
          讀取文件

          ??????using (StreamReader sr = File.OpenText(path))
          ??????{
          ??????? string s = "";
          ????????while ((s = sr.ReadLine()) != null)
          ????????{
          ???????????Console.WriteLine(s);
          ????????}
          ?????}
          ?????//
          刪除 / 拷貝

          ???? try
          ?????{
          ????????File.Delete(path);
          ????????File.Copy(path, @"f:\tt.txt");
          ?????}
          ?????catch (Exception e)
          ?????{
          ????????Console.WriteLine("The process failed: {0}", e.ToString());
          ?????}
          ???}
          }

          方式 2

          //
          流文件(二進制)操作
          private const string name = "Test.data";
          public static void Main(String[] args)
          {
          ????//
          打開文件 ()??, 或通過 File 創(chuàng)建立如: fs = File.Create(path, 1024)

          ????FileStream fs = new FileStream(name, FileMode.CreateNew);
          ????//
          轉換為字節(jié) 寫入數據 ( 可寫入中文 )

          ????Byte[] info = new UTF8Encoding(true).GetBytes("This is some text in the file.");
          ????//
          字節(jié)數組 , 字節(jié)偏移量 , 最多寫入的字節(jié)數

          BinaryWriter?? w?? =?? new?? BinaryWriter(fs);

          // 設置要寫入的偏移量?

          fs.Position=fs.Length;
          ??//??fs.Write(info, 0, info.Length);?? 這個也可以
          ????w.Close();
          ????fs.Close();
          ????//
          打開文件

          ????fs = new FileStream(name, FileMode.Open, FileAccess.Read);
          ????//
          讀取

          ????BinaryReader r = new BinaryReader(fs);
          ????for (int i = 0; i < 11; i++)
          ????{
          ????????Console.WriteLine(r.ReadInt32());
          ????}
          ????w.Close();
          ????fs.Close();

          在將一系列二進制數如方式 1 寫入到 file.txt( 二進制 ) 文件后,打開 file.txt 后顯示的數據二進制數有些錯誤,有些正確。(與存入的不一樣) 向文件中寫入的 默認(也可以設置) 都是使用 UTF-8 編碼 。打開 file.txt 是也是默認 UTF-8 編碼。

          ?

          若將其如方式 2 存入 二進制文件,則顯示的數據一致。若將二進制數(整數)保存為文本文件出錯。二進制文件是直接寫入文件的(磁盤)沒有經過編碼和讀取時的解碼

          posted on 2006-10-24 10:31 注銷..... 閱讀(8259) 評論(0)  編輯  收藏 所屬分類: 閱讀摘要
          主站蜘蛛池模板: 漳州市| 汾阳市| 汨罗市| 喀什市| 杂多县| 松江区| 彭州市| 育儿| 巫溪县| 九江市| 安陆市| 日喀则市| 通河县| 禹城市| 铜鼓县| 岳西县| 上栗县| 胶州市| 左权县| 土默特右旗| 吴堡县| 金川县| 邳州市| 大连市| 商南县| 玉溪市| 龙泉市| 托里县| 吉安市| 大连市| 阳西县| 唐山市| 广州市| 清原| 枣强县| 尤溪县| 上饶市| 屯昌县| 米易县| 双峰县| 东安县|