Unity工具
大家好,我是心疼你的一切,会不定时更新Unity开发技巧,觉得有用记得一键三连哦。
在开发中会遇到各种文件类型,这次是txt文件类型,简单记录一下,方便以后使用
读取txt有好几种方式,有File类、FileStream类、StreamReader类、StreamWriter类
读取txt的类很多,读取的形式也很多,有的整篇读取,有的一行一行读取。按需使用
新建一个文本文档测试Datatxt.txt
切记要设置编码格式为UTF-8格式
要不然中文会乱码
使用这个类读取的文件,文件不能放在StreamingAssets文件夹里,要不然无法拖拽到脚本上面
文本内容如下
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TextAssetsRead : MonoBehaviour
{
public TextAsset text;
// Start is called before the first frame update
void Start()
{
Debug.Log(text.text);
}
}
可以全部打印出来,读取所有内容
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
public class File_ReadAllText : MonoBehaviour
{
public string path=Application.streamingAssetsPath ;
// Start is called before the first frame update
void Start()
{
string strtext = File.ReadAllText(path + "/Datatxt.txt");
Debug.Log(strtext);
}
}
这也是全部读取内容
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
public class File_ReadAllLines : MonoBehaviour
{
public string path = Application.streamingAssetsPath;
// Start is called before the first frame update
void Start()
{
string[]strtext = File.ReadAllLines(path + "/Datatxt.txt");
foreach (var item in strtext)
{
Debug.Log(item );
}
}
}
以上两个都可以设置格式打开文档 Encoding设置格式
文件流形式读取文档
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEngine;
public class FileStream_Read : MonoBehaviour
{
public string path = Application.streamingAssetsPath;
// Start is called before the first frame update
void Start()
{
//文件流形式读取文档
using (FileStream fs = new FileStream(path + "/Datatxt.txt", FileMode.Open, FileAccess.Read))
{
byte[] bytes = new byte[fs.Length];
fs.Read(bytes, 0, bytes.Length);
fs.Close();
string str = Encoding.UTF8.GetString(bytes);
Debug.Log(str);
}
}
}
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEngine;
public class File_OpenRead : MonoBehaviour
{
public string path = Application.streamingAssetsPath;
// Start is called before the first frame update
void Start()
{
//文件流形式读取文档
using (FileStream fs = File.OpenRead(path + "/Datatxt.txt"))
{
byte[] bytes = new byte[fs.Length];
fs.Read(bytes, 0, bytes.Length);
fs.Close();
string str = Encoding.UTF8.GetString(bytes);
Debug.Log(str);
}
}
}
上面两种结果都是一样的,差别有一点点
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEngine;
public class StreamReader_Read : MonoBehaviour
{
public string path = Application.streamingAssetsPath;
// Start is called before the first frame update
void Start()
{
//流形式读取文档
using (StreamReader sr = new StreamReader(path + "/Datatxt.txt"))
{
string strs = sr.ReadToEnd();
sr.Close();
sr.Dispose();
Debug.Log(strs);
}
}
}
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
public class File_OpenText : MonoBehaviour
{
public string path = Application.streamingAssetsPath;
// Start is called before the first frame update
void Start()
{
//流形式读取文档
using (StreamReader sr = File.OpenText(path + "/Datatxt.txt"))
{
string strs = sr.ReadToEnd();
sr.Close();
sr.Dispose();
Debug.Log(strs);
}
}
}
结果一样的,差别也是有的,但不多
上面用:File.ReadAllText 和 File.ReadAllLines读取的
写入用: File.WriteAllText 和 File.WriteAllLines 写入的
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
public class File_WriteAllText : MonoBehaviour
{
public string path = Application.streamingAssetsPath;
// Start is called before the first frame update
void Start()
{
//File类写入文档
File.WriteAllText(path+ "/Datatxt.txt", "测试测试测试测试测试测试测试测试测试测试");
}
}
结果如下
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
public class File_WriteAllLines : MonoBehaviour
{
public string path = Application.streamingAssetsPath;
// Start is called before the first frame update
void Start()
{
//File类写入文档
string[] strs = { "测试数据1", "测试数据2", "测试数据3", "测试数据4", "测试数据5" };
File.WriteAllLines(path + "/Datatxt.txt", strs);
}
}
最后好像会写入一行空的,不过不影响,读取的时候判断一下就行了
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEngine;
public class FileStream_Write : MonoBehaviour
{
public string path = Application.streamingAssetsPath;
// Start is called before the first frame update
void Start()
{
string content = "测试文档测试文档测试文档测试文档测试文档测试文档";
//文件流形式写入文档
using (FileStream fs = new FileStream(path + "/Datatxt.txt", FileMode.Open, FileAccess.Write))
{
byte[] bytes = Encoding.UTF8.GetBytes(content);
fs.Write(bytes, 0, bytes.Length);
fs.Close();
}
}
}
有读就有写,设计的如此完美,哎,就是这么好用
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEngine;
public class StreamWriter_Write : MonoBehaviour
{
public string path = Application.streamingAssetsPath;
// Start is called before the first frame update
void Start()
{
string content = "测试斯柯达监控及安第三季度看金库";
//流形式写入文档
using (StreamWriter sr = new StreamWriter(path + "/测试Datatxt.txt"))
{
//Write写入之后最后不会有空的一行
//sr.Write(content);
//WriteLine 写入之后最后一行有空的一行
sr.WriteLine(content);
sr.Close();
sr.Dispose();
}
}
}
添加一下解释Write和WriteLine
writeLine:将要输出的字符串与换行控制字符一起输出,当次语句执行完毕时候,光标会移到目前输出字符串的下一行。
write:光标会停在输出字符串的最后一个字符,不会移动到下一行。
所以就会出现以上结果一个一行一个两行
本文使用了File类读取/写入,文件流读取/写入,流文件读取/写入
记录一下,以便忘记
不定时更新Unity开发技巧,觉得有用记得一键三连哦。