一、文件流
在 C# 中提供了一个文件流类 FileStream 类,它主要作用是用于读写文件的细节
我们之前学过的 File 只能团体读写文件,而 FileStream 可以以读写字节的形式处置惩罚文件
我们可以通过 FileStream 一部门一部门的读写数据流
好比我可以先存一个 int(4 个字节)再存一个 bool(1 个字节)再存一个 string(n 个字节),利用 FileStream 可以以流式逐个读写
- 类名:FileStream
- 必要引用定名空间:System.IO
二、打开或创建指定文件
(一)new FileStream
- 参数一:路径
- 参数二:打开模式
- CreateNew:创建新文件 假如文件存在 则报错
- Create:创建文件,假如文件存在 则覆盖
- Open:打开文件,假如文件不存在 报错
- OpenOrCreate:打开或者创建文件根据现真相况操作
- Append:若存在文件,则打开并查找文件尾,或者创建一个新文件
- Truncate:打开并清空文件内容
- 参数三:访问模式
- 参数四:共享权限
- None:推辞共享
- Read:允许别的程序读取当前文件
- Write:允许别的程序写入该文件
- ReadWrite:允许别的程序读写该文件
- FileStream fs = new FileStream(Application.dataPath + "/Lesson3.tang", FileMode.Create, FileAccess.ReadWrite);
复制代码 (二)File.Create
- 参数一:路径
- 参数二:缓存巨细
- 参数三:形貌怎样创建或覆盖该文件(不常用)
- Asynchronous:可用于异步读写
- DeleteOnClose:不在利用时,自动删除
- Encrypted:加密
- None:不应用别的选项
- RandomAccess:随机访问文件
- SequentialScan:重新到尾顺序访问文件
- WriteThrough:通过中心缓存直接写入磁盘
- FileStream fs2 = File.Create(Application.dataPath + "/Lesson3.tang");
复制代码 (三)File.Open
- FileStream fs3 = File.Open(Application.dataPath + "/Lesson3.tang", FileMode.Open);
复制代码 三、重要属性和方法
- FileStream fs = File.Open(Application.dataPath + "Lesson3.tang", FileMode.OpenOrCreate);
- // 文本字节长度
- print(fs.Length);
- // 是否可读
- if(fs.CanRead) { }
- // 是否可写
- if(fs.CanWrite) { }
- // 将字节写入文件 当写入后 一定执行一次
- fs.Flush();
- // 关闭流 当文件读写完毕后 一定执行
- fs.Close();
- // 缓存资源销毁回收
- fs.Dispose();
复制代码 四、读写字节
(一)写入字节
- FileStream fs = new FileStream(Application.persistentDataPath + "/Lesson3.tang", FileMode.OpenOrCreate, FileAccess.Write);
- byte[] bytes = BitConverter.GetBytes(999);
- // 方法:Write
- // 参数一:写入的字节数组
- // 参数二:数组中的开始索引
- // 参数三:写入多少个字节
- fs.Write(bytes, 0, bytes.Length);
- // 写入字符串时
- bytes = Encoding.UTF8.GetBytes("唐老狮哈哈哈哈");
- // 先写入长度
- fs.Write(BitConverter.GetBytes(bytes.Length), 0, 4);
- // 再写入字符串具体内容
- fs.Write(bytes, 0, bytes.Length);
- // 避免数据丢失 一定写入后要执行的方法
- fs.Flush();
- // 销毁缓存 释放资源
- fs.Dispose();
复制代码 (二)读取字节
- FileStream fs2 = File.Open(Application.persistentDataPath + "/Lesson3.tang", FileMode.Open, FileAccess.Read);
- // 读取第一个整型
- byte[] bytes2 = new byte[4];
- // 参数一:用于存储读取的字节数组的容器
- // 参数二:容器中开始的位置
- // 参数三:读取多少个字节装入容器
- // 返回值:当前流索引前进了几个位置
- int index = fs2.Read(bytes2, 0, 4);
- int i = BitConverter.ToInt32(bytes2, 0);
- print("取出来的第一个整数" + i); // 999
- print("索引向前移动" + index + "个位置");
- // 读取第二个字符串
- // 读取字符串字节数组长度
- index = fs2.Read(bytes2, 0, 4);
- print("索引向前移动" + index + "个位置");
- int length = BitConverter.ToInt32(bytes2, 0);
- // 要根据我们存储的字符串字节数组的长度 来声明一个新的字节数组 用来装载读取出来的数据
- bytes2 = new byte[length];
- index = fs2.Read(bytes2, 0, length);
- print("索引向前移动" + index + "个位置");
- //得到最终的字符串 打印出来
- print(Encoding.UTF8.GetString(bytes2));
- fs2.Dispose();
复制代码- FileStream fs3 = File.Open(Application.persistentDataPath + "/Lesson3.tang", FileMode.Open, FileAccess.Read);
- // 一开始就申明一个 和文件字节数组长度一样的容器
- byte[] bytes3 = new byte[fs3.Length];
- fs3.Read(bytes3, 0, (int)fs3.Length);
- fs3.Dispose();
- // 读取整数
- print(BitConverter.ToInt32(bytes3, 0));
- // 读取字符串字节数组的长度
- int length2 = BitConverter.ToInt32(bytes3, 4);
- // 得到字符串
- print(Encoding.UTF8.GetString(bytes3, 8, length2));
- fs3.Dispose();
复制代码 五、利用 using 读取文件
using 关键字重要用法:
- using (申明一个引用对象) {
- // 使用对象
- }
复制代码 无论发生什么情况,当 using 语句块竣事后,会自动调用该对象的烧毁方法,制止忘记烧毁或关闭流
using 是一种更安全的利用方法
夸大:目前我们对文件流举行操作,为了文件操作安全,都用 using 来举行处置惩罚最好
比方:
- using (FileStream fs3 = File.Open(Application.persistentDataPath + "/Lesson3.tang", FileMode.Open, FileAccess.Read)) {
- // 一开始就申明一个 和文件字节数组长度一样的容器
- byte[] bytes3 = new byte[fs3.Length];
- fs3.Read(bytes3, 0, (int)fs3.Length);
- fs3.Dispose();
-
- // 读取整数
- print(BitConverter.ToInt32(bytes3, 0));
-
- // 读取字符串字节数组的长度
- int length2 = BitConverter.ToInt32(bytes3, 4);
-
- // 得到字符串
- print(Encoding.UTF8.GetString(bytes3, 8, length2));
- }
复制代码 通过 FileStream 读写时一定要注意:读的规则一定是要和写是同等的
我们存储数据的先后顺序是我们制定的规则,只要按照规则读写就能保证数据的精确性
来源:https://blog.csdn.net/zheliku/article/details/127525143
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |