Unity文件IO

阅读: 评论:0

Unity文件IO

Unity文件IO

文件系统与IO
文件系统对应类 System.IO
DriveInfo : 有关驱动器信息的类
主要方法:GetDrives 用于取得本计算机中所有驱动器信息对象
File: 有关对文件整体操作及内容读写的类
整体操作:

Create :创建文件
Delete: 删除文件
Move:移动文件(剪切)
Copy: 复制文件
Exists: 检查设备中是否有该文件
读写文件:
ReadAllText : 一次将文件中所有内容读入内存,并转为字符串
ReadAllLines: 一次将文件中所有内容读入内存,转为字符串,将字符串按行形成一个字符串数组
ReadLines: 并不会主动文件中的内容,只有再迭代时才读一行,每迭代一次向下读一行.
ReadAllBytes: 将文件中的内容一次读入内存,以字节数组的方式保存WriteAllText: 一次写入大段文本。
WriteAllLines: 每写一行之后写一个换行符.
WriteAllBytes: 一次写入大量字节

FileInfo : 有关对文件整体操作及内容读写的类
与File不同的地方:比File类多一个Length属性,通过该属性可以了解一个文件所占有字节
Directory :有关对文件夹整体操作的类

常用方法:
CreateDirectory:创建文件夹
Delete:删除文件夹
Move:移动文件夹(不能跨驱动器移动)
GetFiles: 取得文件夹下所有的文件名
GetDirectoies:取得文件夹下所有的子文件夹名
Exists:检查某文件夹在设备中是否存在

DirectoryInfo 功能同Directory,只是所有成员为实例成员
Path类,专用于对路径字符串的处理

常用方法:
GetFileName :取得文件名及扩展名
GetFileNameWithOutExtension: 取得文件名不要扩展名
GetDirectoryName : 取得路径中目录的名称
Combine: 将多个字符串合并成为一个路径
/// <summary>
/// 文件夹处理
/// </summary>
public class DirectoryTest : MonoBehaviour
{//在D盘创建文件夹private string myPath = @"D:/aa/bb";private void Start(){DirectoryInfo info = new DirectoryInfo(myPath);//判断路径中是否有文件夹if (!info.Exists)info.Create();//if (info.Exists)//   info.Delete(true);string myPath02 = Path.Combine(myPath, @"cc");//@"D:/aa/bb/cc"DirectoryInfo info02 = new DirectoryInfo(myPath02);if (!info02.Exists)info02.Create();if (info02.Exists)info.Delete(true);/*if (!Directory.Exists(myPath))Directory.CreateDirectory(myPath);if (Directory.Exists(myPath))Directory.Delete(myPath);*/}
}private string filePath = @"D:/";private void Start(){//1.在D盘创建一个名字为Directory的文件夹,并创建一个文件。string dirPath = Path.GetDirectoryName(filePath);if (!Directory.Exists(dirPath))Directory.CreateDirectory(dirPath);if (!File.Exists(filePath))File.Create(filePath);//2.(先在D盘放一个文件)判断若存在,复制到D盘Directory文件夹下。if (File.Exists(@"D:/"))File.Copy(@"D:/", @"D:/", true);//3.(先在D盘放一个Directory02文件夹,并创建几个子文件夹)//通过脚本打印出D盘Directory02文件夹下所有子文件夹的目录名string[] path = Directory.GetDirectories(@"D:/Directory02");foreach (var dirName in path){print(dirName);}}/// <summary>
/// 文件读写
/// </summary>
public class FileTest02 : MonoBehaviour
{private string filePath = @"D:/";private void Start(){//File.WriteAllText(filePath, " Hello aaaaaaa");//写入一大段文本内容//File.WriteAllLines(filePath, new string[] { "Hello", "Hi~", "hhhh" });//string file = File.ReadAllText(filePath);//读取一大段文本内容string[] file = File.ReadAllLines(filePath);foreach(string read in file){print("读到的内容 : " + read);}}}/// <summary>
/// 文件处理
/// </summary>
public class FileTest : MonoBehaviour
{private string filePath = @"D:/aa/";//文件路径private string filePathTo = @"E:/cc";private void Start(){//取得路径中目录的名称string dirPath = Path.GetDirectoryName(filePath);if (!Directory.Exists(dirPath))Directory.CreateDirectory(dirPath);if (!File.Exists(filePath))File.Create(filePath);//剪切文件 新路径:@"E:/"string newFilePath = Path.Combine(filePathTo,Path.GetFileName(filePath));if (!Directory.Exists(filePathTo))Directory.CreateDirectory(filePathTo);if (!File.Exists(newFilePath))File.Move(filePath, newFilePath);}}

数据流 IO:Stream

流涉及三个基本操作:
1.	可以读取流(Read)。读取是从流到数据结构(如字节数组)的数据传输。
2.	可以写入流(Write)。写入是从数据结构到流的数据传输。
3.	流可以支持查找(Seek、Position)。查找是对流内的当前位置进行的查询和修改。查找功能取决于流具有的后备存储区类型。例如,网络流没有当前位置的统一概念,因此一般不支持查找。
FileStream FileMode
StreamReader:流内容读取器用于对数据流或本地文件简单的以字符的方式进行读取,常用方法:ReadLine Read ReadToEnd
StreamWriter:将内容写入流,写入器用于对数据流或本地文件简单的以字符的方式进行写入,常用方法:Write WriteLine
public class StreamTest : MonoBehaviour
{private string path = @"D:/";private string text = null;private void OnGUI(){if (GUILayout.Button("读取内容"))text = ReadFunc(path);GUILayout.Label("内容 : " + text);}private string ReadFunc(string filePath){//创建流对象Stream stream = new FileStream(path, FileMode.Open);byte[] data = new byte[stream.Length];//读取流中所有字节stream.Read(data, 0, (int)stream.Length);//写入文件stream.Write(data, 0, (int)stream.Length);stream.Flush();//写完内容,需刷新//流用完需要关闭,否则资源得不到释放stream.Close();//解码return Encoding.UTF8.GetString(data);}}

读取配置文件

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;/// <summary>
/// 读取配置文件
/// </summary>
public class ConfigTest : MonoBehaviour
{private Dictionary<string, Dictionary<string, string>> dic= new Dictionary<string, Dictionary<string, string>>();private string path;private void Start(){//读取StreamingAssets文件下的文件路径path = Path.Combine(Application.streamingAssetsPath,&#");LoadConfig();}//读取所有数据private void LoadConfig(){string[] lines = null;if (File.Exists(path)){lines = File.ReadAllLines(path);BuildDic(lines);}}//处理所有数据private void BuildDic(string[] lines){//主键string mainKey = null;//子键string subKey = null;//值string subValue = null;foreach (var item in lines){string line = null;line = item.Trim();//去除空行if (!string.IsNullOrEmpty(line)){//取主键if (line.StartsWith("[")){mainKey = line.Substring(1, line.IndexOf("]") - 1);dic.Add(mainKey, new Dictionary<string, string>());}//取子键以及值else{var configValue = line.Split('=');subKey = configValue[0].Trim();subValue = configValue[1].Trim();subValue = subValue.StartsWith(""") ? subValue.Substring(1) : subValue;dic[mainKey].Add(subKey, subValue);}}}}private void OnGUI(){GUILayout.Label(dic["Login"]["Password"]);}
}//配置文件内容
[Game]  
Version=1[Login]
Account = 账号不输入,怎么给你保存数据呀
Password = 没有密码,无法进入游戏
Success = 账号注册成功,赶紧登陆游戏爽去吧
Faild = 账号创建失败,赶快重新创建去吧,一会服务器满了,你就不能和其他小伙伴愉快玩耍啦
Successful = 登陆成功
LoginFaild = 账号或密码错误,请重新输入!!![Job]
Job1 = 战士
Job2 = 法师
Description1 = "    具有强大的近战攻击力,一直活跃在战斗的最前方。强大的力量和充沛的体力让他可以摧毁敌人的一切防御。(推荐新手使用)
Description2 = "    将所有的精力都用在魔法奥义上,每当他向空中举手的时候,不可预测的灾难即将到来。由于自身体力较弱,法师靠法术远距离攻打敌人。
Weapon1 = 砍刀
Weapon2 = 法杖[Money]
Gold = 100
Diamond = 11.2[Scene]
LoadFailed = 场景加载失败,重新加载~

本文发布于:2024-02-02 19:34:32,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170687367045969.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:文件   Unity   IO
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23