在计算机中,所有的数据都是以二进制的形式存储其中的,我们有时候需要将Unity代码中的某些数据以二进制的形式存储在硬盘中,就要进行二进制的数据持久化。
第一要先了解到,C#中各数据类型与字节码的转换,先让我们看一张关于各数据类型所占的字节大小。
C#在其中给我们提供了一个公共类用于字节转换,为此需要引用命名空间BitConverter,用于数据进行字节来回转换。
下面就是将int 类型转字节又将字节转会int类型
//1.各类型转字节
byte[] bytes = BitConverter.GetBytes(98);
//第一个参数表示需要转换的字节数组
//第二个参数表示从字节数组的第几个索引开始转换
int i = BitConverter.ToInt32(bytes,0);
print(i);
但是这种方法无法将字符串和字节数组进行转换,为此C#同样提供了一个类用于字符串的转换
Encoding,为此我们需要引用命名空间System.Text。
下面是将字符串字节的来回转换
//1.将字符串已指定编码格式转字节
byte[] bytes2 = Encoding.UTF8.GetBytes("墨晴轩");
//2.字节数组以指定编码格式转字符串
string s = Encoding.UTF8.GetString(bytes2);
在了解了如何转换字符以后我们还需要了解C#如何去操作文件夹File,为此我们需要引用System.IO这个命名空间。
代码操作文件API常用的一般有:
1.判断文件是否存在
2.创建文件
3.写入文件
4.读取文件
扫描二维码关注公众号,回复:
15569404 查看本文章
//1.判断文件是否存在
if (File.Exists(Application.dataPath + "/UnityTeach.mqx"))
{
print("文件存在");
}
else
{
print("文件不存在");
}
//2.创建文件
//FileStream fs = File.Create(Application.dataPath + "/UnityTeach.mqx");
//3.写入文件
//#将指定字节数组 写入到指定路径的文件中
byte[] bytes = BitConverter.GetBytes(999);
File.WriteAllBytes(Application.dataPath + "/UnityTeach.mqx", bytes);
//#将指定的string数组内容 一行行写入到指定路径中
string[] strs = new string[] {"123","墨晴轩","asdfafasdf"};
File.WriteAllLines(Application.dataPath + "/UnityTeach2.mqx", strs);
//#将指定字符串写入指定路径
File.WriteAllText(Application.dataPath + "/UnityTeach3.mqx", "墨晴轩\n嘉然嘉心糖");
//4.读取文件
//#读取字节数据
bytes = File.ReadAllBytes(Application.dataPath + "/UnityTeach.mqx");
print(BitConverter.ToInt32(bytes, 0));
上面的File文件类能够读、写、创、判文件,但是File能够提供的几乎都是对于文件整体的操作,但是我们又会由去读写一部分数据的需求,这个时候就需要数据流。C#给我们提供了一个FileStream数据流类(需要System.IO这个命名空间)用来实现这一需求,进行一部分地去读写数据。下面我来看看如何使用FileStream。
FileStream:
1.打开或创建指定文件(都返回一个FileStream类型)
有三种的方法,代码如下
//方法一:new FileStream
//参数一:路径
//参数二:打开模式
//CreatNew:创建新文件 如果文件存在 则报错
//Creat:创建文件,如果文件存在则覆盖
//Open:打开文件,如果文件不存在 报错
//OpenOrCreat:打开或者创建文件根据实际情况操作
//Append:若存在文件,则打开并查找文件尾,或者创建一个新文件
//Truncate:打开并清空文件内容
//参数三:访问模式
//参数四:共享权限
FileStream fs = new FileStream(Application.dataPath + "/Lesson3.mqx", FileMode.Create, FileAccess.ReadWrite);
//方法二:File.Creat
//参数一:路径
//参数二:缓存大小
//参数三:描述如何创建或者覆盖该文件
FileStream fs2 = File.Create(Application.dataPath + "/Lesson3.mqx");
//方法三:File.Open
//参数一:路径
//参数二:打开模式
FileStream fs3 = File.Open(Application.dataPath + "/Lesson3.mqx", FileMode.Open);
2.FileStream重要的方法和属性
FileStream fs = File.Open(Application.dataPath + "/Lesson3.mqx", FileMode.OpenOrCreate);
文本字节长度
print(fs.Length);
//是否可读
if (fs.CanRead)
{
}
if (fs.CanWrite)
{
}
//将字节写入文件 当写入后 一定执行一次
fs.Flush();
//关闭流 当文件读写完毕后 一定执行
fs.Close();
//缓存资源销毁回收
fs.Dispose();
3.写入字节
FileStream fs = new FileStream(Application.persistentDataPath + "/Lesson3.mqx", FileMode.OpenOrCreate,FileAccess.Write);
byte[] bytes = BitConverter.GetBytes(1024);
byte[] data= new byte[1024];
//方法:Write
//参数一:写入的字节数组
//参数二:数组中的开始索引
//参数三:写入多少个字节
fs.Write(bytes, 0, bytes.Length);
fs.Write(data, 100, 2);
//写入字符串时
bytes = Encoding.UTF8.GetBytes("关注嘉然,顿顿解馋");
//先写长度
int length=bytes.Length;
fs.Write(BitConverter.GetBytes(length),0,4);
//再写入字符串具体内容
fs.Write(bytes, 0, bytes.Length);
//避免数据丢失 一定写入后要执行的方法
fs.Flush();
//销毁缓存 释放资源
fs.Dispose();
4.读取字节
FileStream fs2 = File.Open(Application.persistentDataPath + "/Lesson3.mqx", FileMode.Open, FileAccess.Read);
#region 挨个读取字节数组
//读取第一个整型
byte[] bytes2 = new byte[4];
//参数一:用于存储读取的字节数组的容器
//参数二:容器中开始的位置
//参数三:读取多少个字节装入容器
//返回值:当前流索引前进了几个位置
int index = fs2.Read(bytes2, 0, 4);
int i = BitConverter.ToInt32(bytes2,0);
print("取出来的第一个整数" + i);//1024
print("索引向前移动了" + index + "个位置");
//读取第二个字符串
//读取字符串字节数组长度
index = fs2.Read(bytes2, 0, 4);
print("索引向前移动了" + index + "个位置");
length = BitConverter.ToInt32(bytes2, 4);
//要根据存储的字符串字节数组的长度 来声明一个新的数组
//bytes2 = new byte[length];
index = fs2.Read(bytes2, 0, length);
print("索引向前移动了" + index + "个位置");
//得到最终的字符串 打印
print(Encoding.UTF8.GetString(bytes2));
fs.Dispose();
#endregion
#region 一次性读取再挨个读取
Debug.Log("*********************************");
FileStream fs3 = File.Open(Application.persistentDataPath + "/Lesson3.mqx", FileMode.Open, FileAccess.Read);
//一开始就声明一个 和文件字节数组长度一样的容器
byte[] bytes3 = new byte[fs.Length];
fs3.Read(bytes3, 0, (int)fs3.Length);
fs3.Dispose();
//读取整数
print(BitConverter.ToInt32(bytes3, 0));
//得出去字符串字节数组的长度
int length2 = BitConverter.ToInt32(bytes3, 4);
//得到字符串
Encoding.UTF8.GetString(bytes3, 8, length2);
以上就是转字节码、文件操作、文件流的基础