• 您的位置:首頁 > 新聞動態(tài) > Unity3D

    UNITY3D 創(chuàng)建、讀取、寫入、修改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()函數(shù),將這個文檔按照行行進行部讀取:


    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]);
            }
        }
    }
    
    上面兩個函數(shù)都各自有個重載函數(shù):



    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);

    可以以設(shè)定的文檔格式打開文檔。


    以文件流的形式讀取文檔


    通過IO命名空間下的FileStream類進行讀取文檔數(shù)據(jù):

    這是第種方式,通過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()函數(shù)加載文檔:



    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()函數(shù)以流形式讀取文檔:



    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);
            }
        }
    }

    修改數(shù)據(jù)保存文檔  通過File類寫入數(shù)據(jù)

    還記得怎么讀取數(shù)據(jù)嗎?
    File.ReadAllText()函數(shù)及ReadAllLines()函數(shù)
    那么寫入數(shù)據(jù)就使用:
    File.WriteAllText()函數(shù)及ReadWriteLines()函數(shù)



    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, "測試數(shù)據(jù)");
        }
    }
    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 = { "測試數(shù)據(jù)1", "測試數(shù)據(jù)2", "測試數(shù)據(jù)3" };
            File.WriteAllLines(path, content);
        }
    }
    
    WriteAllText()是將整個文本保存到文檔中。
    ReadWriteLines()函數(shù)是將個string數(shù)組保存到文檔中。



    通過文件流的形式寫入數(shù)據(jù)

    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();
            }
        }
    }

    通過流形式寫入數(shù)據(jù)

    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命名空間。


    本文來自網(wǎng)絡(luò),版權(quán)歸原作者所有。