unity對TXT文本的寫入與讀取

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;  //操作文件夾時需引用該命名空間
using System.Text;

public class TxtWriteAndRead : MonoBehaviour
{
    TextAsset m_Txt;

    void Start()
    {
        // AddTxtTextByFileStream("第一種方法添加text文本");

        // AddTxtTextByFileInfo("第二種方法添加txt文本");

        //重寫數(shù)據(jù)
        // ReWriteMyTxtByFileStreamTxt();

        // ReadTxtFirst();
        //ReadTxtSecond();
        //ReadTxtThird();
        //ReadTxtForth();

        ReadTxtFifth();
    }

    /// <summary>
    /// 創(chuàng)建txt 方法一
    /// </summary>
    /// <param name="txtText"></param>
    public void AddTxtTextByFileStream(string txtText)
    {
        string path = Application.dataPath + "/Json/MyTxtByFileStream.txt";
        // 文件流創(chuàng)建一個文本文件
        FileStream file = new FileStream(path, FileMode.Create);
        //得到字符串的UTF8 數(shù)據(jù)流
        byte[] bts = System.Text.Encoding.UTF8.GetBytes(txtText);
        // 文件寫入數(shù)據(jù)流
        file.Write(bts, 0, bts.Length);
        if (file != null)
        {
            //清空緩存
            file.Flush();
            // 關(guān)閉流
            file.Close();
            //銷毀資源
            file.Dispose();
        }
    }

    /// <summary>
    /// 可以重新寫入文件到一個已存在的txt文本中去
    /// </summary>
    void ReWriteMyTxtByFileStreamTxt()
    {
        string path = Application.dataPath + "/Json/MyTxtByFileStream.txt";

        string[] strs = { "123", "321", "234" };

        File.WriteAllLines(path, strs);

        /*
         文本內(nèi)容為:(逐行顯示的)
         123
         321
         234
         原文本內(nèi)容為:
         第一種方法添加text文本
         */

        //如果想在某一行中添加一行新的,可以將原txt文本讀取下來,保存到數(shù)組里,
        //然后新建一個數(shù)組,載將原數(shù)組文本與新加入的行數(shù)據(jù)同時寫入到新數(shù)組里
        //然后用新數(shù)組數(shù)據(jù)替換(重寫)原來的數(shù)據(jù)
    }

    /// <summary>
    /// 創(chuàng)建txt 方法二
    /// </summary>
    /// <param name="txtText"></param>
    public void AddTxtTextByFileInfo(string txtText)
    {
       string path = Application.dataPath + "/MyInfo.txt";
        StreamWriter sw;
        FileInfo fi = new FileInfo(path);

        if (!File.Exists(path))
        {
            sw = fi.CreateText();
        }
        else {
            sw = fi.AppendText();   //在原文件后面追加內(nèi)容      
        }
        sw.WriteLine(txtText);
        sw.Close();
        sw.Dispose();
    }

    /// <summary>
    /// 讀取txt文本方法一
    /// 需要將txt文本放到Resources文件夾下讀取
    /// </summary>
    void ReadTxtFirst()
    {
        m_Txt = Resources.Load<TextAsset>("readTxt");
        string str = m_Txt.text.ToString();
        print(str);  //txt文本數(shù)據(jù)讀取
    }

    /// <summary>
    /// 讀取txt文本方法二
    /// 逐行讀取 該方法可以單獨獲取某一行的數(shù)據(jù)
    /// </summary>
    void ReadTxtSecond()
    {
        string path = Application.dataPath + "/Json/MyTxtByFileInfo.txt";
        //逐行讀取返回的為數(shù)組數(shù)據(jù)
        string[] strs = File.ReadAllLines(path);

        foreach (string item in strs)
        {
            print(item); 
        }
    }

    /// <summary>
    /// 第三種讀取方法
    /// 需引用  using System.Text;
    /// </summary>
    void ReadTxtThird()
    {
        string path = Application.dataPath + "/Json/MyTxtByFileInfo.txt";

        string str = File.ReadAllText(path, Encoding.UTF8);

        print(str);
    }

    /// <summary>
    /// 第四種讀取方法
    /// </summary>
    void ReadTxtForth()
    {
        string path = Application.dataPath + "/Json/MyTxtByFileInfo.txt";
        //FileStream fsSource = new FileStream(path, FileMode.Open, FileAccess.Read);

        //文件讀寫流
        StreamReader strr = new StreamReader(path);
        //讀取內(nèi)容
        string str = strr.ReadToEnd();
        print(str); 
    }

    /// <summary>
    /// 第五種讀取方法
    /// 文件流方式
    /// </summary>
    void ReadTxtFifth() {
        string path = Application.dataPath + "/Json/MyTxtByFileInfo.txt";
        FileStream files = new FileStream(path, FileMode.Open, FileAccess.Read);
        byte[] bytes = new byte[files.Length];
        files.Read(bytes, 0, bytes.Length);
        files.Close();
        string str = UTF8Encoding.UTF8.GetString(bytes);
        print(str); 
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容