各类型数据和字节数据相互转换
数字类型:
byte[] byte1 = BitConverter.GetBytes(123);
int i = BitConverter.ToInt32(byte1);
字符串类型:
byte[] byte2 = Encoding.UTF8.GetBytes(“Shen”); string s = Encoding.UTF8.GetString(byte2);
字符串类型跟数字类型不一样,是因为字符串转为二进制需要有指定的编码格式
文件操作File类的常用内容
主要是:
File.WriteAllBytes(path,bytes)
File.WriteAllLines(path,string[])
File.WriteAllText(path,string)
read同理
实例:
//1.判断文件是否存在
if(File.Exists(Application.dataPath + "/UnityTeach.tang"))
{
print("文件存在");
}
else
{
print("文件不存在");
}
//2.创建文件
//FileStream fs = File.Create(Application.dataPath + "/UnityTeach.tang");
//3.写入文件
//将指定字节数组 写入到指定路径的文件中
byte[] bytes = BitConverter.GetBytes(999);
File.WriteAllBytes(Application.dataPath + "/UnityTeach.tang", bytes);
//将指定的string数组内容 一行行写入到指定路径中
string[] strs = new string[] { "123", "唐老狮", "123123kdjfsalk", "123123123125243"};
File.WriteAllLines(Application.dataPath + "/UnityTeach2.tang", strs);
//将指定字符串写入指定路径
File.WriteAllText(Application.dataPath + "/UnityTeach3.tang", "唐老狮哈\n哈哈哈哈123123131231241234123");
//4.读取文件
//读取字节数据
bytes = File.ReadAllBytes(Application.dataPath + "/UnityTeach.tang");
print(BitConverter.ToInt32(bytes, 0));
//读取所有行信息
strs = File.ReadAllLines(Application.dataPath + "/UnityTeach2.tang");
for (int i = 0; i < strs.Length; i++)
{
print(strs[i]);
}
//读取所有文本信息
print(File.ReadAllText(Application.dataPath + "/UnityTeach3.tang"));
//5.删除文件
//注意 如果删除打开着的文件 会报错
File.Delete(Application.dataPath + "/UnityTeach.tang");
//6.复制文件
//参数一:现有文件 需要是流关闭状态
//参数二:目标文件
File.Copy(Application.dataPath + "/UnityTeach2.tang", Application.dataPath + "/唐老狮.tanglaoshi", true);
//7.文件替换
//参数一:用来替换的路径
//参数二:被替换的路径
//参数三:备份路径
File.Replace(Application.dataPath + "/UnityTeach3.tang", Application.dataPath + "/唐老狮.tanglaoshi", Application.dataPath + "/唐老狮备份.tanglaoshi");
//8.以流的形式 打开文件并写入或读取
//参数一:路径
//参数二:打开模式
//参数三:访问模式
//FileStream fs = File.Open(Application.dataPath + "/UnityTeach2.tang", FileMode.OpenOrCreate, FileAccess.ReadWrite);
文件流操作
方法一:new FileStream 参数一:路径 参数二:打开模式 CreateNew:创建新文件 如果文件存在 则报错 Create:创建文件,如果文件存在 则覆盖 Open: 打开文件,如果文件不存在 报错 OpenOrCreate: 打开或者创建文件根据实际情况操作 Append:若存在文件,则打开并查找文件尾,或者创建一个新文件 Truncate:打开并清空文件内容 参数三:访问模式 参数四:共享权限 None 谢绝共享 Read 允许别的程序读取当前文件 Write 允许别的程序写入该文件 ReadWrite 允许别的程序读写该文件 FileStream fs = new FileStream(path, FileMode.Create, FileAccess.ReadWrite);
方法二:File.Create 参数一:路径 参数二:缓存大小 参数三:描述如何创建或覆盖该文件(不常用) Asynchronous 可用于异步读写 DeleteOnClose 不在使用时,自动删除 Encrypted 加密 None 不应用其它选项 RandomAccess 随机访问文件 SequentialScan 从头到尾顺序访问文件 WriteThrough 通过中间缓存直接写入磁盘 FileStream fs2 = File.Create(path);
方法三:File.Open 参数一:路径 参数二:打开模式 FileStream fs3 = File.Open(path);
将字节写入文件 当写入后 一定执行一次 fs.Flush();
关闭流 当文件读写完毕后 一定执行 fs.Close();
缓存资源销毁回收 fs.Dispose();
读取写入因为使用了文件流 所以开始索引0就是在上一次操作过后的第一位索引位置,并不是整个文件的第一个索引位置,索引像流一样往后走
写入字节
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();
读取字节
方法一:挨个读取字节数组
//读取第一个整形 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();
方法二:一次性读取再挨个读取
//一开始就申明一个 和文件字节数组长度一样的容器 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));
文件夹操作
类名:Directory
Directory.Exists(path) // 判断文件是否存在
Directory.CreateDirectory(path); // 创建文件夹,返回值为DirectoryInfo,可以借此得到该文件夹相关信息
Directory.GetDirectories(path); // 获得所有子文件夹,返回srting[ ]
Directory.GetFiles(path); // 获得所有子文件,同上
DirectoryInfo和FileInfo
DirectoryInfo dInfo = Directory.CreateDirectory(path);
FileInfo[] fInfos = dInfo.GetFiles();
fInfos[i].Name //文件名 fInfos[i].FullName //路径 fInfos[i].Length //字节长度 fInfos[i].Extension //后缀名
C#类对象序列化
using会在结束时自动调用dispose,dispose会调用close和flush,所以使用了using就无需手动调用这些
序列化的类需要加上特性
[System.Serializable]
1.内存流存储
using (MemoryStream ms = new MemoryStream()) { //2进制格式化程序 BinaryFormatter bf = new BinaryFormatter(); //序列化对象 生成2进制字节数组 写入到内存流当中 bf.Serialize(ms, p); //得到对象的2进制字节数组 byte[] bytes = ms.GetBuffer(); //存储字节 File.WriteAllBytes(Application.dataPath + “/Lesson5.tang”, bytes); }
2.文件流存储
using (FileStream fs = new FileStream(Application.dataPath + “/Lesson5_2.tang”, FileMode.OpenOrCreate, FileAccess.Write)) { //2进制格式化程序 BinaryFormatter bf = new BinaryFormatter(); //序列化对象 生成2进制字节数组 写入到文件流当中 bf.Serialize(fs, p); }
C#类对象反序列化
1.文件流读取
using (FileStream fs = File.Open(Application.dataPath + “/Lesson5_2.tang”, FileMode.Open, FileAccess.Read)) { //申明一个 2进制格式化类 BinaryFormatter bf = new BinaryFormatter(); //反序列化 Person p = bf.Deserialize(fs) as Person; }
2.内存流读取(网络传输过来字节)
//目前没有网络传输 我们还是直接从文件中获取 byte[] bytes = File.ReadAllBytes(Application.dataPath + “/Lesson5_2.tang”); //申明内存流对象 一开始就把字节数组传输进去 using (MemoryStream ms = new MemoryStream(bytes)) { //申明一个 2进制格式化类 BinaryFormatter bf = new BinaryFormatter(); //反序列化 Person p = bf.Deserialize(ms) as Person;
}
读取Excel
需要先导入相关dll包
主要步骤:
IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(fs); // 从文件流中读取
DataSet result = excelReader.AsDataSet(); // 读取出的内容转化为DataSet
得到result后,一个excel又会分为多个table,在通过对应的table中的Rows,Columns得到具体内容
示例:
[MenuItem("GameTool/读取Excel里的具体信息")]
private static void ReadExcel()
{
using (FileStream fs = File.Open(Application.dataPath + "/ArtRes/Excel/PlayerInfo.xlsx", FileMode.Open, FileAccess.Read))
{
IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(fs);
DataSet result = excelReader.AsDataSet();
for (int i = 0; i < result.Tables.Count; i++)
{
DataTable table = result.Tables[i];
for (int j = 0; j < table.Rows.Count; j++)
{
row = table.Rows[j];
for (int k = 0; k < table.Columns.Count; k++)
{
Debug.Log(row[k].ToString());
}
}
}
}
}

评论(0)
暂无评论