Unity3D学习笔记(⼆⼗五):⽂件操作
⽂件是什么?
存储在硬盘上的最后的节点。
⽂件夹是什么?
⽂件的上级单位称为⽂件夹。
⽂件夹的基本结构?
⽂件夹是层级化结构的,对于同级的⽂件夹不可以重名,⽗⽂件夹和⼦⽂件夹可以同名》
IO:I是Input输⼊,O是Output输出
IO流:指数据的输⼊输出流。
命名空间:using System.IO;
Directory⽂件夹类:操作⽂件夹的类,静态类,静态⽅法,⼯具类,不能实例化,通过类名调⽤
API:Directory.GetCurrentDirectory();//获取当前应⽤程序(⼯程)的绝对路径
API:Directory.Exists()//;判断指定的⽂件夹路径是否存在
API:Directory.CreateDirectory();//创建⽂件夹
API:Directory.Delete();//删除⽂件夹
API:Directory.Move();//移动⽂件夹,可以通过这种⽅式来实现⽂件夹的重命名
API:获取指定路径下所有⽂件夹和⽂件的绝对路径的数组
API:Directory.GetFiles();//获取⽂件夹下的所有⽂件
API:Directory.GetDirectories();//获取指定路径下的所有⽂件夹
API:获得创建时间,最后访问和最后修改时间
GetCreationTime,GetLastAccessTime,GetLastWriteTime
API:设置创建时间,最后访问和最后修改时间
SetCreationTime,SetLastAccessTime,SetLastWriteTime
ascii文件夹怎么创建
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
public class LessonFile : MonoBehaviour {
// Use this for initialization
void Start () {
//判断⽂件是否存在
if (File.Exists(@"D:\桌⾯\"))
{
Debug.Log("⽂件存在");
}
//删除⽂件,注意⽂件的后缀
File.Delete(@"D:\桌⾯\");
//移动⽂件,可以通过这种⽅式来对⽂件进⾏重命名,注意后缀名
//⽂件名可以不同,后缀名也可以不同(改变⽂件的类型)
File.Move(@"D:\桌⾯\Directory\JPG.jpg", @"D:\桌⾯\Directory\PNG.png");
//拷贝⽂件,前后名字可以不⼀致,后缀名也可以不⼀致
File.Copy(@"D:\桌⾯\Directory\PNG.png", @"D:\桌⾯\Directory\GIF.gif");
//创建⽂件
FileStream fsCreate = File.Create(@"D:\桌⾯\");
fsCreate.Close();//关闭⽂件流
}
// Update is called once per frame
void Update () {
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
public class LessonDirectory : MonoBehaviour {
/
/ Use this for initialization
void Start () {
//获取当前应⽤程序(⼯程)的绝对路径
Directory.GetCurrentDirectory();
Debug.Log(Directory.GetCurrentDirectory());
//@取消转义
string str = @"\";
Debug.Log(str);
//判断指定的⽂件夹路径是否存在
if (Directory.Exists(@"D:\桌⾯\Directory"))
{
Debug.Log("Directory⽂件夹存在");
}
//创建⽂件夹
Directory.CreateDirectory(@"D:\桌⾯\Directory\File1");
Directory.CreateDirectory(@"D:\桌⾯\Directory\File2");
//删除⽂件夹,对于⽂件夹⾥有⽂件的情况,如果使⽤⼀个参数去删除会报错
//对于⽂件夹⾥有⽂件的情况,我们使⽤重载的第⼆个参数传⼊true表⽰强制删除
Directory.Delete(@"D:\桌⾯\Directory\File1");
//移动⽂件夹,可以通过这种⽅式来实现⽂件夹的重命名
Directory.Move(@"D:\桌⾯\Directory\File2", @"D:\桌⾯\Directory\File3");
//获取⽂件夹下的所有⽂件
string[] files = Directory.GetFiles(@"D:\桌⾯\Directory");
foreach (var item in files)
{
Debug.Log(item);
}
//获取指定路径下的所有⽂件夹
string[] directorys = Directory.GetDirectories(@"D:\桌⾯");
foreach (var item in directorys)
{
Debug.Log(item);
}
}
// Update is called once per frame
void Update () {
}
}
@取消转义
str="\n":默认情况下,“\”在字符串中表⽰转义符
str=@"\n":如果⼀个字符串前加了@符号,表⽰取消转义;那么“\”将作为字符存在,不代表转移符
"\\":单斜杠“\”在字符串⾥是转义符,双斜杠"\\"在字符串⾥表⽰单斜杠“\”
File⽂件类
命名空间:using System.Text;
API:File.Create();//创建⽂件
API:File.Open();//打开⽂件
FileMode
----Open:打开⼀个已有的⽂件,保证⽂件路径是正确的
----Create:创建⼀个新⽂件,如果该⽂件已经存在,那么新⽂件覆盖掉旧⽂件
----CreateNew:创建⼀个新⽂件,但是得保证该⽂件是不存在的,如果存在会报错
----OpenOrCreate:如果存在即打开⽂件,如果不存在即创建新⽂件
----Append:打开⽂件,并且⽂件的游标在最后,追加模式
创建⽂件,打开⽂件,返回的都是⽂件流
打开⼀个⽂件流之后,⼀定要关闭,否则会占⽤
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
public class LessonFile : MonoBehaviour {
// Use this for initialization
void Start () {
//判断⽂件是否存在
if (File.Exists(@"D:\桌⾯\"))
{
Debug.Log("⽂件存在");
}
/
/删除⽂件,注意⽂件的后缀
File.Delete(@"D:\桌⾯\");
//移动⽂件,可以通过这种⽅式来对⽂件进⾏重命名,注意后缀名
//⽂件名可以不同,后缀名也可以不同(改变⽂件的类型)
File.Move(@"D:\桌⾯\Directory\JPG.jpg", @"D:\桌⾯\Directory\PNG.png");
//拷贝⽂件,前后名字可以不⼀致,后缀名也可以不⼀致
File.Copy(@"D:\桌⾯\Directory\PNG.png", @"D:\桌⾯\Directory\GIF.gif");
//创建⽂件
FileStream fsCreate = File.Create(@"D:\桌⾯\");
fsCreate.Close();//关闭⽂件流
}
/
/ Update is called once per frame
void Update () {
}
}
字节数组的传递是引⽤传递,改变当前⽂本的值会改变原有⽂本的值
读取⽂本⽂件时:解析字节数组时,需要使⽤对应的编码格式
字符编码种类
1、ASCII:ASCII码是西欧编码的⽅式,采取7位编码,所以是2^7=128,共可以表⽰128个字符,包括34个字符,(如换⾏LF,回车CR等),其余94位为英⽂字母和标点符号及运算符号等。
2、GB2321:GB2312 是对 ASCII 的中⽂扩展。兼容ASCII。
3、GBK:GBK 兼容ASCLL 兼容 GB2312 是GB2312的扩展。
4、Unicode:Unicode是国际组织制定的可以容纳世界上所有⽂字和符号的字符编码⽅案。
5、UTF-8:UTF-8以字节为单位对Unicode进⾏编码。
乱码:
1、使⽤了不同的编码格式进⾏解析⽂本
2、读取的内容少了,也有可能出现乱码
UTF-8,3个字节表⽰1个汉字,缺少3个字节会少1个汉字,缺少2个字节会乱码
关闭⽂件的问题:代码运⾏中出错,没有执⾏最后关闭⽂件的代码
Try Catch:
先依次执⾏Try语句块的代码,⼀但发现有错误,不会继续执⾏Try⾥的代码,直接跳到Catch块⾥依次执⾏Catch块⾥的代码,Catch块⾥的代码执⾏完继续向下执⾏。如果Try⾥的代码没有错误,不会执⾏Catch⾥的代码,直接执⾏Catch块后的代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LessonTryCatch : MonoBehaviour {
public GameObject obj;
// Use this for initialization
void Start () {
Debug.Log("开始try");
try
{
Debug.Log("try 语句块!");
Debug.Log(obj.name);//这句话⼀定会报错,跳转catch
Debug.Log("打印名字");
}
catch (System.Exception e)
{
Debug.Log("catch 语句块!" + e);
Debug.Log("错误信息:" + e);
Debug.Log(obj.name);//如果catch报错,之后的代码都不会执⾏
}
Debug.Log("结束try");
}
// Update is called once per frame
void Update () {
}
}
作业:读取⽂本内容,显⽰在UI的text组件上,并实现内容滚动
⽂本位置调整
⽂本⾃动扩容
读取⽂件
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Text;
using UnityEngine.UI;
public class LessonHomework : MonoBehaviour {
public const string filePath = @"D:\桌⾯\1.txt";
private Text text;
private void Awake()
{
text = transform.Find("Mask/Text").GetComponent<Text>();
}
// Use this for initialization
void Start () {
FileStream fs = File.Open(filePath, FileMode.Open);
try
{
byte[] bytes = new byte[fs.Length];
int ret = fs.Read(bytes, 0, (int)fs.Length);
string str = Encoding.UTF8.GetString(bytes);
< = str;
}
catch (System.Exception e)
{
Debug.Log(e.ToString());
}
fs.Close();
}
// Update is called once per frame
void Update () {
}
}
写⼊⽂件
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Text;
public class LessonWriteFile : MonoBehaviour {
public const string filePath = @"D:\桌⾯\";
// Use this for initialization
void Start () {
//string filePath1 = Directory.GetCurrentDirectory();
//filePath1 = filePath1 + "\\Directory\\";
//写⼊⽂本⽂件
//1、打开⽂件
FileStream fs = File.Open(filePath, FileMode.Open);
// FileStream fs = File.Open(filePath, FileMode.Append);//追加模式
Debug.Log("游标的位置:" + fs.Position);
//第⼀个参数:向前移或向后移多少位,第⼆个参数:从什么位置开始移动
//对于想要把游标移动到⽂件的末尾
//⽅法1
fs.Seek(fs.Length, SeekOrigin.Begin);
//⽅法2
fs.Seek(fs.Length - fs.Position, SeekOrigin.Current);
//⽅法3
fs.Seek(0, SeekOrigin.End);
//对于想要把游标移动到⽂件的开始
//⽅法1
fs.Seek(0, SeekOrigin.Begin);
//⽅法2
fs.Seek(-fs.Position, SeekOrigin.Current);
//⽅法3
fs.Seek(-fs.Length, SeekOrigin.End);
Debug.Log("游标的位置:" + fs.Position);
//为了保证⽂件打开之后⼀定能关闭,⽂件操作写在Try Catch⾥
try
{
//2、把要写⼊的内容转成字节数组
string str = "做⼀只快乐的⼩鱼苗~";
byte[] bytes = Encoding.UTF8.GetBytes(str);
//3、写⼊⽂件
//第⼀个参数:写⼊⽂件的内容转换成的字符串
//第⼆个参数:从字节数组中的第⼏位开始写⼊
//第三个参数:从字节数组中写⼊多少位
fs.Write(bytes, 0, bytes.Length);
}
catch (System.Exception e)
{
Debug.Log(e.ToString());
}
/
/最终关闭⽂件流
fs.Close();
}
// Update is called once per frame
void Update () {
}
}
⽂本读写器
⽂本读
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
public class LessonStreamReader : MonoBehaviour {
public const string filePath = @"D:\桌⾯\";
// Use this for initialization
void Start () {
//⽂本读写器
//打开⽂件,保证路径是正确的,有效的
StreamReader sr = new StreamReader(filePath, System.Text.Encoding.UTF8);
try
{
/
/string str = sr.ReadLine();//只读取⼀⾏。
string str = sr.ReadToEnd();//从开始读到结尾
Debug.Log("读取的内容: " + str);
}
catch (System.Exception e)
{
Debug.Log(e.ToString());
}
//最后⼀定要关闭⽂件
sr.Close();
}
/
/ Update is called once per frame
void Update () {
}
}
⽂本写,不能操作游标,如果不使⽤追加模式,默认清空原有内容,重新写⼊using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
public class LessonStreamWrite : MonoBehaviour {
public const string filePath = @"D:\桌⾯\";
// Use this for initialization
void Start () {
//⽂本读写器
//1、打开⽂件
//这个重载,会把⽂件内容全部清除,重新写⼊内容
//StreamWriter sw = new StreamWriter(filePath);
//这个重载,是追加模式,第⼆个参数指定是否追加,第三个参数是指定编码格式
StreamWriter sw = new StreamWriter(filePath, true, System.Text.Encoding.UTF8);
try
{
sw.Write("Hello World!");
}
catch (System.Exception e)
{
Debug.Log(e.ToString());
}
//最后⼀定关闭⽂件
sw.Close();
}
// Update is called once per frame
void Update () {
}
}
⽂件拷贝
⼩⽂件的直接拷贝
⼤⽂件的线程拷贝
1、使⽤多线程的⽅式,防⽌⽂件过⼤,卡死主线程
2、多线程⾥⾯不能改变需要渲染操作的值(例如改变slider的进度条的值)
3、多线程⾥使⽤while循环,要设置跳出循环的条件
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Threading;
public class LessonCopyFile : MonoBehaviour {
public UnityEngine.UI.Slider slider;
public const string sourceFilePath = @"D:\桌⾯\1.rar";
public const string targetFilePath = @"D:\桌⾯\2.rar";
private float maxValue = 1f;
private float currentValue;
private bool isStop = false;
// Use this for initialization
void Start () {
//⼩⽂件的拷贝
//CopyFile(sourceFilePath, targetFilePath);
/
/⼤⽂件的拷贝
//TestCopyFile();
//⼤⽂件的线程拷贝
//1.申请⼀个Thread的对象
Thread th = new Thread(TestCopyFile);
//2.启动这个线程
th.Start();
}
// Update is called once per frame
void Update () {
//滑动条赋值
}
private void OnDestroy()
{
isStop = true;
}
//⼩⽂件的拷贝
void CopyFile(string sourceFile, string targetFile)
{
//拷贝,就把源⽂件的内容写⼊到新的⽂件中
//1.读取源⽂件的内容
FileStream source = File.Open(sourceFile, FileMode.Open);
byte[] bytes = new byte[source.Length];
try
{
//读取内容
source.Read(bytes, 0, bytes.Length);
}
catch (System.Exception e)
{
Debug.Log(e.ToString());
}
source.Close();
//2.写⼊新⽂件
FileStream target = File.Open(targetFile, FileMode.Create);
try
{
//写⼊新⽂件
target.Write(bytes, 0, bytes.Length);
}
catch (System.Exception e)
{
Debug.Log(e.ToString());
}
target.Close();
}
void TestCopyFile()
{
Debug.Log("开始拷贝");
CopyBigFile(sourceFilePath, targetFilePath);
Debug.Log("拷贝结束");
}
void CopyBigFile(string sourceFile, string targetFile)
{
FileStream source = new FileStream(sourceFile, FileMode.Open);
FileStream target = new FileStream(targetFile, FileMode.Create);
byte[] bytes = new byte[100];//限制每次拷贝100 * 1024个字节⼤⼩的内容
maxValue = source.Length;
try
{
long readCount = 0;
//必须保证所有的内容都写⼊到另⼀个⽂件中了, 跳出循环
//只要保证每次读取的长度的累加值⼤于等于⽂件流的⼤⼩
while (readCount < source.Length)
{
int length = source.Read(bytes, 0, bytes.Length);
target.Write(bytes, 0, length);
readCount += length;
currentValue = readCount;
//当组件销毁时,停⽌多线程⾥的while循环,防⽌程序停⽌运⾏时,多线程仍在执⾏                if (isStop)
{
break;
}
}
}
catch (System.Exception e)
{
Debug.Log(e.ToString());
}
source.Close();
target.Close();
}
}
图⽚加载并显⽰UI组件中
⽅法⼀:
1、把图⽚转换成字节数组
2、把字节数组转换成图⽚,loadImage ⽅法⼆:
1、本地加载,加file:\\
2、⽹络加载,使⽤协程

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