• 您的位置:首页 > 新闻动态 > Unity3D

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


    本文来自网络,版权归原作者所有。