UNITY3D 創建、讀取、寫入、修改TXT文本文件
2022/9/11??????點擊:
通過TextAsset類讀取文檔
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Demo : MonoBehaviour { public TextAsset texttest; void Start() { Debug.Log(texttest.text); } }
通過File類讀取文件
using System.Collections; using System.Collections.Generic; using System.IO; using UnityEngine; public class Demo : MonoBehaviour { void Start() { string textTxtpath = File.ReadAllText(Application.streamingAssetsPath + "/test.txt"); Debug.Log(textTxtpath); } }
也可以使用File類的ReadAllLines()函數,將這個文檔按照一行一行進行全部讀取:
using System.Collections; using System.Collections.Generic; using System.IO; using UnityEngine; public class Demo5 : MonoBehaviour { void Start() { string[] textTxt = File.ReadAllLines(Application.streamingAssetsPath + "/TextRead.txt"); for (int i = 0; i < textTxt.Length; i++) { Debug.Log(textTxt[i]); } } }上面兩個函數都各自有一個重載函數:
public static string[] ReadAllLines(string path); public static string[] ReadAllLines(string path, Encoding encoding); public static string ReadAllText(string path, Encoding encoding); public static string ReadAllText(string path);
可以以設定的文檔格式打開文檔。
以文件流的形式讀取文檔
通過IO命名空間下的FileStream類進行讀取文檔數據:
這是第一種方式,通過FileStream類的實例化方法去加載文件:
using System.Collections; using System.Collections.Generic; using System.IO; using System.Text; using UnityEngine; public class Demo5 : MonoBehaviour { void Start() { string path = Application.streamingAssetsPath + "/TextRead.txt"; //文件流形式讀取文檔 using (FileStream fs = new FileStream(path, 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); } } }還可以通過File類的OpenRead()函數加載文檔:
using System.Collections; using System.Collections.Generic; using System.IO; using System.Text; using UnityEngine; public class Demo5 : MonoBehaviour { void Start() { string path = Application.streamingAssetsPath + "/TextRead.txt"; //文件流形式讀取文檔 using (FileStream fs = File.OpenRead(Application.streamingAssetsPath + "/TextRead.txt")) { byte[] bytes = new byte[fs.Length]; fs.Read(bytes, 0, bytes.Length); fs.Close(); string str = Encoding.UTF8.GetString(bytes); Debug.Log(str); } } }
以流形式讀取文檔
通過IO命名空間下的StreamReader類以流形式讀取文檔:
using System.Collections; using System.Collections.Generic; using System.IO; using System.Text; using UnityEngine; public class Demo5 : MonoBehaviour { void Start() { string path = Application.streamingAssetsPath + "/TextRead.txt"; //流形式讀取文檔 using (StreamReader sr = new StreamReader(path)) { string content = sr.ReadToEnd(); sr.Close(); sr.Dispose(); Debug.Log(content); } } }還可以使用File類的OpenText()函數以流形式讀取文檔:
using System.Collections; using System.Collections.Generic; using System.IO; using System.Text; using UnityEngine; public class Demo5 : MonoBehaviour { void Start() { string path = Application.streamingAssetsPath + "/TextRead.txt"; //流形式讀取文檔 using (StreamReader sr = File.OpenText(path)) { string content = sr.ReadToEnd(); sr.Close(); sr.Dispose(); Debug.Log(content); } } }
修改數據保存文檔 通過File類寫入數據
還記得怎么讀取數據嗎?File.ReadAllText()函數及ReadAllLines()函數
那么寫入數據就使用:
File.WriteAllText()函數及ReadWriteLines()函數
using System.Collections; using System.Collections.Generic; using System.IO; using System.Text; using UnityEngine; public class Demo5 : MonoBehaviour { void Start() { string path = Application.streamingAssetsPath + "/TextRead.txt"; File.WriteAllText(path, "測試數據"); } }
using System.Collections; using System.Collections.Generic; using System.IO; using System.Text; using UnityEngine; public class Demo5 : MonoBehaviour { void Start() { string path = Application.streamingAssetsPath + "/TextRead.txt"; string[] content = { "測試數據1", "測試數據2", "測試數據3" }; File.WriteAllLines(path, content); } }WriteAllText()是將整個文本保存到文檔中。
ReadWriteLines()函數是將一個string數組保存到文檔中。
通過文件流的形式寫入數據
using System.Collections; using System.Collections.Generic; using System.IO; using System.Text; using UnityEngine; public class Demo5 : MonoBehaviour { void Start() { string path = Application.streamingAssetsPath + "/TextRead.txt"; string content = "測試文檔"; //文件流形式讀取文檔 using (FileStream fs = new FileStream(path, 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 Demo5 : MonoBehaviour { void Start() { string path = Application.streamingAssetsPath + "/TextRead.txt"; string content = "測試文檔"; using (StreamWriter sr = new StreamWriter(path)) { sr.WriteLine(content); sr.Close(); sr.Dispose(); Debug.Log(content); } } }這些讀取的操作都需要引入IO命名空間。
本文來自網絡,版權歸原作者所有。
- 上一篇:Json文件操作(創建、讀取、解析、修改) 2022/9/11
- 下一篇:WISEGLOVE19FE+元宇宙數據手套發布 2021/11/22