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

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

    • 分享

      C# byte及char FileStream 與StreamReader的區(qū)別

       orion360doc 2011-01-25
      FileStream對(duì)象表示在磁盤或網(wǎng)絡(luò)路徑上指向文件的流。這個(gè)類提供了在文件中讀寫字節(jié)的方法,但經(jīng)常使用StreamReader或 StreamWriter執(zhí)行這些功能。這是因?yàn)?span style="BACKGROUND-COLOR: yellow">FileStream類操作的是字節(jié)和字節(jié)數(shù)組,而Stream類操作的是字符數(shù)據(jù)。這是這兩種類的一個(gè)重要區(qū)別,如果你是準(zhǔn)備讀取byte數(shù)據(jù)的話,用StreamReader讀取然后用 System.Text.Encoding.Default.GetBytes轉(zhuǎn)化的話,如下,則可能出現(xiàn)數(shù)據(jù)丟失的情況,如byte數(shù)據(jù)的個(gè)數(shù)不對(duì)等。因此操作byte數(shù)據(jù)時(shí)要用FileStream。
      string textContent = fileStream.ReadToEnd();byte[] bytes = System.Text.Encoding.Default.GetBytes(textContent);
      字符數(shù)據(jù)易于使用, 但是有些操作,比如隨機(jī)文件訪問(訪問文件中間某點(diǎn)的數(shù)據(jù)),就必須由FileStream對(duì)象執(zhí)行.
      其中創(chuàng)建FileStream對(duì)象最簡(jiǎn)單的構(gòu)造函數(shù)如下:
      1        FileStream file = new FileStream(fileName,FileMode.Member);
      2        FileStream file = new FileStream(fileName, FileMode.Member, FileAccess.Member);
      而FileAccess的成員:

      成 員

      說 明

      Read

      打開文件,用于只讀

      Write

      打開文件,用于只寫

      ReadWrite

      打開文件,用于讀寫

       

      對(duì)文件進(jìn)行不是FileAccess枚舉成員指定的操作會(huì)導(dǎo)致拋出異常。此屬性的作用是,基于用戶的身份驗(yàn)證級(jí)別改變用戶對(duì)文件的訪問權(quán)限。

      在FileStream構(gòu)造函數(shù)不使用FileAccess枚舉參數(shù)的版本中,使用默認(rèn)值FileAccess. ReadWrite。

      FileMode枚舉成員,使用每個(gè)值會(huì)發(fā)生什么,取決于指定的文件名是否表示已有的文件。

      成 員

      文 件 存 在

      文件不存在

      Append

      打開文件,流指向文件的末尾,只能與枚舉FileAccess.Write聯(lián)合使用

      創(chuàng)建一個(gè)新文件。只能與枚舉FileAccess.Write聯(lián)合使用

      Create

      刪除該文件,然后創(chuàng)建新文件

      創(chuàng)建新文件

      CreateNew

      拋出異常

      創(chuàng)建新文件

      Open

      打開現(xiàn)有的文件,流指向文件的開頭

      拋出異常

      OpenOrCreate

      打開文件,流指向文件的開頭

      創(chuàng)建新文件

      Truncate

      打開現(xiàn)有文件,清除其內(nèi)容。流指向文件的開頭,保留文件的初始創(chuàng)建日期

      拋出異常


      FileStream類操作的是字節(jié)和字節(jié)數(shù)組,而Stream類操作的是字符數(shù)據(jù)
      StreamWriter允許將字符和字符串寫入文件,它處理底層的轉(zhuǎn)換,向FileStream對(duì)象寫入數(shù)據(jù)。StreamReader也類似。

      實(shí)例:
        1using System;
        2using
       System.Data;
        3using
       System.IO;
        4using
       System.Text;
        5

        6
      /// <summary>
        7/// Summary description for FileReadAndWrite
        8/// </summary>

        9public class FileReadAndWrite
       10
      {
       11    public
       FileReadAndWrite()
       12    
      {
       13        //

       14        // TODO: Add constructor logic here
       15        
      //
       16    }

       17    /// <summary>
       18    /// 用FileStream寫文件
       19    /// </summary>

       20    /// <param name="str"></param>
       21    /// <returns></returns>

       22    public void FileStreamWriteFile(string str)
       23    
      {
       24        byte
      [] byData;
       25        char
      [] charData;
       26        try

       27        {
       28            FileStream nFile = new FileStream("love.txt"
      , FileMode.Create);
       29            //獲得字符數(shù)組

       30            charData = str.ToCharArray();
       31            //初始化字節(jié)數(shù)組

       32            byData = new byte[charData.Length];
       33            //將字符數(shù)組轉(zhuǎn)換為正確的字節(jié)格式

       34            Encoder enc = Encoding.UTF8.GetEncoder();
       35            enc.GetBytes(charData, 0, charData.Length,byData,0,true
      );
       36            nFile.Seek(0
      , SeekOrigin.Begin);
       37            nFile.Write(byData, 0
      , byData.Length);
       38        }

       39        catch (Exception ex)
       40        
      {
       41            throw
       ex;
       42        }

       43    }

       44    /// <summary>
       45    /// FileStream讀取文件
       46    /// </summary>

       47    /// <param name="filePath"></param>
       48    /// <returns></returns>

       49    public string FileStreamReadFile(string filePath)
       50    
      {
       51        byte[] data = new byte[100
      ];
       52        char[] charData = new char[100
      ];
       53        try

       54        {
       55            FileStream file = new
       FileStream(filePath, FileMode.Open);
       56            //文件指針指向0位置

       57            file.Seek(0, SeekOrigin.Begin);
       58            //讀入兩百個(gè)字節(jié)

       59            file.Read(data, 0200);
       60            //提取字節(jié)數(shù)組

       61            Decoder dec = Encoding.UTF8.GetDecoder();
       62            dec.GetChars(data, 0, data.Length, charData, 0
      );
       63        }

       64        catch (Exception ex)
       65        
      {
       66            throw
       ex;
       67        }

       68        return Convert.ToString(charData);
       69    }

       70    /// <summary>
       71    /// StreamWriter寫文件
       72    /// </summary>

       73    public void StreamWriterWriteFile()
       74    
      {
       75        try

       76        {
       77            FileStream nFile = new FileStream("love.txt"
      , FileMode.CreateNew);
       78            StreamWriter writer = new
       StreamWriter(nFile);
       79

       80            writer.WriteLine("I love You!"
      );
       81            writer.WriteLine("Do you love me!"
      );
       82
                  writer.Close();
       83        }

       84        catch
       85        { }
       86    }

       87    /// <summary>
       88    /// StreamReader讀取文件
       89    /// </summary>

       90    /// <returns></returns>

       91    public string StreamReaderReadFile()
       92    
      {
       93        string str=""
      ;
       94        try

       95        {
       96            FileStream file = new FileStream("love.txt"
      , FileMode.Open);
       97            StreamReader sr = new
       StreamReader(file);
       98            while (sr.ReadLine()!=null
      )
       99            
      {
      100                str +=
       sr.ReadLine();
      101            }

      102            //或者str = sr.ReadToEnd();
      103            sr.Close();
      104        }

      105        catch
      106        { }
      107        return str;
      108    }

      109}

      110

        本站是提供個(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)論公約

        類似文章 更多