stream的Read、Write方法实例

using (FileStream outStream = new FileStream(@"D:12.txt", FileMode.Open))
            {
               
using (FileStream fs = new FileStream(@"D:1.txt", FileMode.Open))
                {
                   
//缓冲区太小的话,速度慢而且伤硬盘
                   
//声明一个4兆字节缓冲区大小,比如迅雷也有一个缓冲区,如果没有缓冲区的话,                     //每下载一个字节都要往磁盘进行写,非常伤磁盘,所以,先往内存的缓冲区写字节,当
                   
//写够了一定容量之后,再往磁盘进行写操作,减低了磁盘操作。
                    byte[] bytes = new byte[100];
                   
int readBytes;
                   
//第二个参数Offset表示当前位置的偏移量,一般都传0
                    fs.Seek(100, SeekOrigin.Current);
                   
if ((readBytes = fs.Read(bytes, 0, bytes.Length)) > 0) //读取的位置自动往后挪动。
                    {
                       
//readBytes为实际读到的byte数,因为最后一次可能不会读满。
                        if (outStream.CanSeek == true)
                            outStream.Seek(
100, SeekOrigin.Current);
                            outStream.Write(bytes,
8, readBytes-10);//8为偏移量,10为数量
                    }
                }
            }

原文地址:https://www.cnblogs.com/dongyl/p/3595582.html