注銷

          注銷

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

          ?

          1 .?dāng)?shù)值應(yīng)保存在二進(jìn)制文件

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

          方式 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

          //
          流文件(二進(jìn)制)操作
          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);
          ????//
          轉(zhuǎn)換為字節(jié) 寫入數(shù)據(jù) ( 可寫入中文 )

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

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

          // 設(shè)置要寫入的偏移量?

          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();

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

          ?

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

          posted on 2006-10-24 10:31 注銷..... 閱讀(8255) 評論(0)  編輯  收藏 所屬分類: 閱讀摘要
          主站蜘蛛池模板: 嵩明县| 涞源县| 天长市| 苏尼特左旗| 靖安县| 翁牛特旗| 都昌县| 锦州市| 青神县| 名山县| 岳阳县| 孟津县| 科尔| 扶风县| 东明县| 木里| 天峨县| 吴江市| 南溪县| 乌拉特中旗| 凤山市| 泰和县| 吴堡县| 博客| 宁远县| 阿勒泰市| 克什克腾旗| 阳江市| 龙陵县| 黎城县| 太湖县| 泸溪县| 新邵县| 叶城县| 江达县| 县级市| 浪卡子县| 青海省| 沙坪坝区| 新巴尔虎左旗| 东至县|