[Unity学习教程] 2022-10-26 Unity 2进制2——文件流操作

[复制链接]
查看1207 | 回复0 | 2023-8-23 12:02:33 | 显示全部楼层 |阅读模式 来自 中国北京
一、文件流

​ 在 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:允许别的程序读写该文件

  1. FileStream fs = new FileStream(Application.dataPath + "/Lesson3.tang", FileMode.Create, FileAccess.ReadWrite);
复制代码
(二)File.Create


  • 参数一:路径
  • 参数二:缓存巨细
  • 参数三:形貌怎样创建或覆盖该文件(不常用)

    • Asynchronous:可用于异步读写
    • DeleteOnClose:不在利用时,自动删除
    • Encrypted:加密
    • None:不应用别的选项
    • RandomAccess:随机访问文件
    • SequentialScan:重新到尾顺序访问文件
    • WriteThrough:通过中心缓存直接写入磁盘

  1. FileStream fs2 = File.Create(Application.dataPath + "/Lesson3.tang");
复制代码
(三)File.Open


  • 参数一:路径
  • 参数二:打开模式
  1. FileStream fs3 = File.Open(Application.dataPath + "/Lesson3.tang", FileMode.Open);
复制代码
三、重要属性和方法

  1. FileStream fs = File.Open(Application.dataPath + "Lesson3.tang", FileMode.OpenOrCreate);
  2. // 文本字节长度
  3. print(fs.Length);
  4. // 是否可读
  5. if(fs.CanRead) { }
  6. // 是否可写
  7. if(fs.CanWrite) { }
  8. // 将字节写入文件 当写入后 一定执行一次
  9. fs.Flush();
  10. // 关闭流 当文件读写完毕后 一定执行
  11. fs.Close();
  12. // 缓存资源销毁回收
  13. fs.Dispose();
复制代码
四、读写字节

(一)写入字节
  1. FileStream fs = new FileStream(Application.persistentDataPath + "/Lesson3.tang", FileMode.OpenOrCreate, FileAccess.Write);
  2. byte[] bytes = BitConverter.GetBytes(999);
  3. // 方法:Write
  4. // 参数一:写入的字节数组
  5. // 参数二:数组中的开始索引
  6. // 参数三:写入多少个字节
  7. fs.Write(bytes, 0, bytes.Length);
  8. // 写入字符串时
  9. bytes = Encoding.UTF8.GetBytes("唐老狮哈哈哈哈");
  10. // 先写入长度
  11. fs.Write(BitConverter.GetBytes(bytes.Length), 0, 4);
  12. // 再写入字符串具体内容
  13. fs.Write(bytes, 0, bytes.Length);
  14. // 避免数据丢失 一定写入后要执行的方法
  15. fs.Flush();
  16. // 销毁缓存 释放资源
  17. fs.Dispose();
复制代码
(二)读取字节

  • 方法一:挨个读取字节
  1. FileStream fs2 = File.Open(Application.persistentDataPath + "/Lesson3.tang", FileMode.Open, FileAccess.Read);
  2. // 读取第一个整型
  3. byte[] bytes2 = new byte[4];
  4. // 参数一:用于存储读取的字节数组的容器
  5. // 参数二:容器中开始的位置
  6. // 参数三:读取多少个字节装入容器
  7. // 返回值:当前流索引前进了几个位置
  8. int index = fs2.Read(bytes2, 0, 4);
  9. int i     = BitConverter.ToInt32(bytes2, 0);
  10. print("取出来的第一个整数" + i); // 999
  11. print("索引向前移动" + index + "个位置");
  12. // 读取第二个字符串
  13. // 读取字符串字节数组长度
  14. index = fs2.Read(bytes2, 0, 4);
  15. print("索引向前移动" + index + "个位置");
  16. int length = BitConverter.ToInt32(bytes2, 0);
  17. // 要根据我们存储的字符串字节数组的长度 来声明一个新的字节数组 用来装载读取出来的数据
  18. bytes2 = new byte[length];
  19. index  = fs2.Read(bytes2, 0, length);
  20. print("索引向前移动" + index + "个位置");
  21. //得到最终的字符串 打印出来
  22. print(Encoding.UTF8.GetString(bytes2));
  23. fs2.Dispose();
复制代码

  • 方法二:一次性读取再挨个读取
  1. FileStream fs3 = File.Open(Application.persistentDataPath + "/Lesson3.tang", FileMode.Open, FileAccess.Read);
  2. // 一开始就申明一个 和文件字节数组长度一样的容器
  3. byte[] bytes3 = new byte[fs3.Length];
  4. fs3.Read(bytes3, 0, (int)fs3.Length);
  5. fs3.Dispose();
  6. // 读取整数
  7. print(BitConverter.ToInt32(bytes3, 0));
  8. // 读取字符串字节数组的长度
  9. int length2 = BitConverter.ToInt32(bytes3, 4);
  10. // 得到字符串
  11. print(Encoding.UTF8.GetString(bytes3, 8, length2));
  12. fs3.Dispose();
复制代码
五、利用 using 读取文件

​ using 关键字重要用法:
  1. using (申明一个引用对象) {
  2.     // 使用对象
  3. }
复制代码
​ 无论发生什么情况,当 using 语句块竣事后,会自动调用该对象的烧毁方法,制止忘记烧毁或关闭流
​ using 是一种更安全的利用方法
​ 夸大:目前我们对文件流举行操作,为了文件操作安全,都用 using 来举行处置惩罚最好
​ 比方:
  1. using (FileStream fs3 = File.Open(Application.persistentDataPath + "/Lesson3.tang", FileMode.Open, FileAccess.Read)) {
  2.     // 一开始就申明一个 和文件字节数组长度一样的容器
  3.     byte[] bytes3 = new byte[fs3.Length];
  4.     fs3.Read(bytes3, 0, (int)fs3.Length);
  5.     fs3.Dispose();
  6.    
  7.     // 读取整数
  8.     print(BitConverter.ToInt32(bytes3, 0));
  9.    
  10.     // 读取字符串字节数组的长度
  11.     int length2 = BitConverter.ToInt32(bytes3, 4);
  12.    
  13.     // 得到字符串
  14.     print(Encoding.UTF8.GetString(bytes3, 8, length2));
  15. }
复制代码
​ 通过 FileStream 读写时一定要注意:读的规则一定是要和写是同等的
​ 我们存储数据的先后顺序是我们制定的规则,只要按照规则读写就能保证数据的精确性

来源:https://blog.csdn.net/zheliku/article/details/127525143
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则