关于C#Unity读写文件

自己随便写的一个关于文本读入和写出的一个文本类

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MyGameFrameWork;
using System.IO;

public class CreateTextContentMgr : BaseManager<CreateTextContentMgr>
{
    
    
    // Start is called before the first frame update
    /// <summary>
    /// 直接创建一个然后写入到txt内容
    /// </summary>
    /// <param name="text"></param>
   public static void AddTextFileStream(string text)
    {
    
    
        string path = Application.dataPath + "/TextFile/TextFile.txt";

        FileStream file = new FileStream(path, FileMode.Create);

        byte[] bts = System.Text.Encoding.UTF8.GetBytes(text);

        file.Write(bts, 0, bts.Length);

        if(file!=null)
        {
    
    
            file.Flush();
            file.Close();
            file.Dispose();
        }
    }

    /// <summary>
    /// 直接追加Text文本上
    /// </summary>
   public static void AddTextAppedInfo(string text)
    {
    
    
        string path = Application.dataPath + "/TextFile/TextFile.txt";

        StreamWriter sw;

        FileInfo fi = new FileInfo(path);

        if(!File.Exists(path))
        {
    
    
            sw = fi.CreateText();
        }
        else
        {
    
    
            sw = fi.AppendText();
        }
        sw.WriteLine(text);
        sw.Close();
        sw.Dispose();
    }


    public static string ReadTxtAllInfo()
    {
    
    
        string path = Application.dataPath + "/TextFile/TextFile.txt";

        StreamReader strr = new StreamReader(path);

        string str = strr.ReadToEnd();

        return str;
    }



}

猜你喜欢

转载自blog.csdn.net/charlsdm/article/details/125043442