首頁  >  文章  >  後端開發  >  c#的FileStream檔案流

c#的FileStream檔案流

黄舟
黄舟原創
2016-12-27 14:03:372610瀏覽

檔案流

FileStream、StreamReader和StreamWriter可以操作大型檔案;
FileStream 操作位元組;可以操作任何類型的檔案;
StreamReader和StreamWriter操作字元;

File       作用                 參數

FileStream()    建立FileStream物件     第一個是路徑,第二個是檔案模式FIleMode枚舉,第三個資料模式FileAcess    

Read()    分部分讀取文件,則傳回實際讀到的有效位元組數,如果讀得數量不是第三個參數指定的,就用空填滿   第一個是存放的位元組數組,表示從哪個地方往數組裡放數組? ,每次最多讀取多少   

Write()    把位元組數組寫入   第一個參數是位元組數組,第二個參數表示從哪個地方開始寫入,第三個參數表述最多寫多少   

close( ),dispose()    關閉流,釋放流所佔用的資源   

FileMode OpenOrCreate , Append

FileAcess. Read 、Write、ReadWirte

將創建文件流對象的過程寫在自動幫助我們釋放

StreamReader和StreamWriter

可以用來讀取格式化文字檔;

有ReadLine和Write WriteLine方法

<code class="language-C# hljs cs">using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace 用FileStream读写文件
{
    class Program
    {
        static void Main(string[] args)
        {
            String str = @"E:\下载\软件";
            Stopwatch sw = new Stopwatch();
            sw.Start();
            using (FileStream fsWriter = new FileStream(str + @"\opencv-3.0.exe", FileMode.Create, FileAccess.Write))
            {
 
                using (FileStream fsReader = new FileStream(str + @"\opencv-2.4.9.exe", FileMode.Open, FileAccess.Read))
                {
                    byte[] bytes=new byte[1024*4];//4kB是合适的;
                    int readNum;
                    while((readNum=fsReader.Read(bytes,0,bytes.Length))!=0)//小于说明读完了
                    {
                        fsWriter.Write(bytes,0,readNum);
                    }
 
 
                }//suing reader
            }//using writer
            sw.Stop();
            Console.WriteLine("总的运行时间为{0}",sw.ElapsedMilliseconds);
            Console.ReadKey();
 
        }//main
    }//class
}
</code>

 以上就是c#的FileStream檔案流的內容,更多相關內容請關注PHP中文網(.www.php. cn)!


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn