• UNITY3D 創(chuàng)建、讀取、寫(xiě)入、修改TXT文本文件

    2022/9/11??????點(diǎn)擊:


    通過(guò)TextAsset類(lèi)讀取文檔

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class Demo : MonoBehaviour
    {
        public TextAsset texttest;
        void Start()
        {
            Debug.Log(texttest.text);
        }
    }

    通過(guò)File類(lèi)讀取文件

    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類(lèi)的ReadAllLines()函數(shù),將這個(gè)文檔按照一行一行進(jìn)行全部讀?。?/span>


    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]);
            }
        }
    }
    
    上面兩個(gè)函數(shù)都各自有一個(gè)重載函數(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è)定的文檔格式打開(kāi)文檔。


    以文件流的形式讀取文檔


    通過(guò)IO命名空間下的FileStream類(lèi)進(jìn)行讀取文檔數(shù)據(jù):

    這是第一種方式,通過(guò)FileStream類(lèi)的實(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 = 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);
            }
        }
    }
    
    還可以通過(guò)File類(lèi)的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);
            }
        }
    }

    以流形式讀取文檔

    通過(guò)IO命名空間下的StreamReader類(lèi)以流形式讀取文檔:



    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類(lèi)的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ù)保存文檔  通過(guò)File類(lèi)寫(xiě)入數(shù)據(jù)

    還記得怎么讀取數(shù)據(jù)嗎?
    File.ReadAllText()函數(shù)及ReadAllLines()函數(shù)
    那么寫(xiě)入數(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, "測(cè)試數(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 = { "測(cè)試數(shù)據(jù)1", "測(cè)試數(shù)據(jù)2", "測(cè)試數(shù)據(jù)3" };
            File.WriteAllLines(path, content);
        }
    }
    
    WriteAllText()是將整個(gè)文本保存到文檔中。
    ReadWriteLines()函數(shù)是將一個(gè)string數(shù)組保存到文檔中。



    通過(guò)文件流的形式寫(xiě)入數(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 = "測(cè)試文檔";
            //文件流形式讀取文檔
            using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Write))
            {
                byte[] bytes = Encoding.UTF8.GetBytes(content);
                fs.Write(bytes, 0, bytes.Length);
                fs.Close();
            }
        }
    }

    通過(guò)流形式寫(xiě)入數(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 = "測(cè)試文檔";
            using (StreamWriter sr = new StreamWriter(path))
            {
                sr.WriteLine(content);
                sr.Close();
                sr.Dispose();
                Debug.Log(content);
            }
        }
    }
    
    這些讀取的操作都需要引入IO命名空間。


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