26.FileStream

FileStream作用

为文件提供流操作,既支持同步读写操作,也支持异步读写操作。

构造方法

public FileStream (string path, System.IO.FileMode mode, System.IO.FileAccess access):使用指定的路径、创建模式和读/写权限初始化 FileStream 类的新实例。

path:当前 FileStream 对象将封装的文件的相对路径或绝对路径。
mode:用于确定文件的打开或创建方式的枚举值之一。
access:枚举值的按位组合,这些枚举值确定 FileStream 对象访问文件的方式。 该常数还可以确定由 FileStream 对象的 CanRead 和 CanWrite 属性返回的值。 如果 path 指定磁盘文件,则 CanSeek 为 true。

Read(Byte[], Int32, Int32):使用FileStream读取数据并写入给定缓冲区中。

参数:
array Byte[]:当此方法返回时,包含指定的字节数组,此数组中 offset 和 (offset + count - 1) 之间的值被从当前源中读取的字节所替换。
offset Int32:array 中的字节偏移量,将在此处放置读取的字节。
count Int32:最多读取的字节数。

namespace Demo {
 
    class Program {

        static void Main(string[] args) {

            //使用FileStream来读取数据
            FileStream fsRead = new FileStream(@"C:\Users\22053\Desktop\new.txt", FileMode.OpenOrCreate, FileAccess.Read);
            byte[] buffer = new byte[1024 * 1024 * 5];
            
            //返回本次实际读取到的有效字节数
            int r = fsRead.Read(buffer, 0, buffer.Length);
            //将字节数组中每一个元素按照指定的编码格式解码成字符串
            string s = Encoding.Default.GetString(buffer, 0, r);
            //关闭流
            fsRead.Close();
            //释放流所占用的资源
            fsRead.Dispose();
            Console.WriteLine(s);
            Console.ReadKey();

        }
    }

}

将创建文件流对象的过程写在using当中,会自动的帮助我们释放流所占用的资源。

Write(Byte[], Int32, Int32):将字节写入文件

参数:
array Byte[]:包含要写入该流的数据的缓冲区。
offset Int32:array 中的从零开始的字节偏移量,从此处开始将字节复制到该流。
count Int32:最多写入的字节数。

namespace Demo {
 
    class Program {

        static void Main(string[] args) {



            //使用FileStream来写入数据
            using (FileStream fsWrite = new FileStream(@"C:\Users\22053\Desktop\new.txt", FileMode.OpenOrCreate, FileAccess.Write)) {
                string str = "看有没有覆盖掉";
                byte[] buffer = Encoding.Default.GetBytes(str);
                fsWrite.Write(buffer, 0, buffer.Length);
            }
            Console.WriteLine("写入OK");
            Console.ReadKey();

        }
    }

}

使用using,会自动的帮助我们释放流所占用的资源。

复制文件并写入到指定的位置

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace Demo {

    class Program {

        static void Main(string[] args) {


            //思路:就是先将要复制的多媒体文件读取出来,然后再写入到你指定的位置
            string source = @"C:\Users\22053\Desktop\03-讲师管理前端开发(添加路由).ev4";
            string target = @"C:\Users\22053\Desktop\new.ev4";
            CopyFile(source, target);
            Console.WriteLine("复制成功");
            Console.ReadKey();

        }

        public static void CopyFile(string soucre, string target) {
            //1、我们创建一个负责读取的流
            using (FileStream fsRead = new FileStream(soucre, FileMode.Open, FileAccess.Read)) {
                //2、创建一个负责写入的流
                using (FileStream fsWrite = new FileStream(target, FileMode.OpenOrCreate, FileAccess.Write)) {
                    byte[] buffer = new byte[1024 * 1024 * 5];
                    //因为文件可能会比较大,所以我们在读取的时候 应该通过一个循环去读取
                    while (true) {
                        //返回本次读取实际读取到的字节数
                        //第二个参数为什么为0?
                        //因为public override int Read (byte[] array, int offset, int count)中
                        //array Byte[]:当此方法返回时,包含指定的字节数组,此数组中 offset 和(offset + count - 1) 之间的值被从当前源中读取的字节所替换。
                        //offset Int32: array 中的字节偏移量,将在此处放置读取的字节。
                        int r = fsRead.Read(buffer, 0, buffer.Length);
                        //如果返回一个0,也就意味什么都没有读取到,读取完了
                        if (r == 0) {
                            break;
                        }
                        fsWrite.Write(buffer, 0, r);
                    }


                }
            }
        }
    }

}

更多内容参考官方文档

猜你喜欢

转载自www.cnblogs.com/lz32158/p/12920209.html
26