FileStream读写文件流

时间:2022-09-04 19:01:50
用FileStream 读取文件流并显示给文件内容
           

             string p = @"C:\Users\Administrator\Desktop\1.txt";
            
            FileStream fsR = new FileStream(p, FileMode.Open, FileAccess.Read);
            byte[] buff = new byte[2];//设置内存缓存区大小
            while (true)
            {
              int r=  fsR.Read(buff, 0, buff.Length);
              if (r == 0)
                  break;
              else
              {                  
               string str=   System.Text.Encoding.Default.GetString(buff);               
               Console.Write(str );
              }
              fsR.Close();//关闭流         fsR.Dispose();//释放资源

  

//FileStream 读写文件流

string p = @"C:\Users\Administrator\Desktop\2.mp4";
           

            using (FileStream fsR = new FileStream(p2, FileMode.Open, FileAccess.Read))
            {
                using (FileStream fsW = new FileStream(p, FileMode.OpenOrCreate, FileAccess.Write))
                {
                    byte[] buff = new byte[1024 * 1024 * 5];//设置缓存区大小为5MB
                    while (true)
                    {
                        int r = fsR.Read(buff, 0, buff.Length);//读取
                        if (r == 0)
                            break;
                        else
                        {
                            fsW.Write(buff, 0, r);//写入
                 }
                    }
                }
            }