文件流

使用文件流读写大文件

 1 //文件流只能单一读取或者写入操作,文件流需要做自动释放,不然占据文件句柄造成其它进程无法访问
 2             //1.、将数据读取到byte数组,2.将byte数组中的数据写入到文件
 3             using (FileStream fsReader = new FileStream(txtBigFile.Text, FileMode.Open))
 4             {
 5                 using (FileStream fsWriter = new FileStream("d:\Temp.wmv", FileMode.Create))
 6                 {
 7                     int count = 0;
 8                     do
 9                     {
10                         byte[] bytes = new byte[1024 * 1024 * 10];
11                         //count就是得到实际读取的字节数
12                         count = fsReader.Read(bytes, 0, bytes.Length);//将数据读取到数组中
13                         //在做写入的时候只写入真正读取到的字节内容
14                         if (count == 0)
15                         {
16                             break; //如果没有读取任何的数据,那么就跳出循环
17                         }
18                         fsWriter.Write(bytes, 0, count);
19                     } while (true);
20                     MessageBox.Show("yes");
21                 }
22             }

文件加密

 1 using (FileStream fsReader = new FileStream(txtBigFile.Text, FileMode.Open))
 2             {
 3                 using (FileStream fsWriter = new FileStream("d:\Temp.wmv", FileMode.Create))
 4                 {
 5                     byte[] bytes = new byte[1024 * 1024 * 10];
 6                     int count = 0;
 7                     do
 8                     {
 9                         count = fsReader.Read(bytes, 0, bytes.Length);
10                         for (int i = 0; i < count; i++)
11                         {
12                             bytes[i] = (byte)(255 - bytes[i]);
13                         }
14                         if (count <= 0)
15                         {
16                             break;
17                         }
18                         fsWriter.Write(bytes, 0, count);
19                     } while (true);
20                 }
21                 MessageBox.Show("ok");
22             }
原文地址:https://www.cnblogs.com/junhuang/p/4420787.html