UInity基础-Unity对TXT文本读取写入删除操作

在Unity开发或者在c#开发中经常会使用txt存储文本信息,需要对文本进行读取操作,写入操作或者是删除操作。记录一下。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Text;
using UnityEngine.UI;
public class TxtWrite : MonoBehaviour
{
    public InputField _inputField;

    // Start is called before the first frame update
    void Start()
    {
        //AddTXTText("2测试文本内容");
       // ReadTxt();
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    public void Btn_Write()
    {
        string str_input = _inputField.text;
        AddTXTText(str_input);
        ReadTxt();
    }//写入
     void AddTXTText(string txtText)
    {
        string path = Application.dataPath + "/StreamingAssets/ShoppCart.txt";
        StreamWriter sw;
        FileInfo fi = new FileInfo(path);
        if (!File.Exists(path))
        {
            sw = fi.CreateText();
        }
        else
        {
            sw = fi.AppendText();
        }
        sw.WriteLine(txtText);
        sw.Close();
        sw.Dispose();
        
    }
    //读取
    void ReadTxt()
    {
        string path = Application.dataPath + "/StreamingAssets/ShoppCart.txt";
        string[] strs = File.ReadAllLines(path);
        foreach (string item in strs)
        {
            print(item);
           // File.Delete(item);
        }
    }
    //删除
    public  void DeleteTxt()
    {
        string path = Application.dataPath + "/StreamingAssets/ShoppCart.txt";
        File.WriteAllText(path,string.Empty);
        print("成功");
    }

}

 

你可能感兴趣的:(unity,基础,文本读取写入删除,c#基础)