unity Txt读取

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Text;
public class My_textio : MonoBehaviour
{
    public static My_textio instance;

    StreamWriter writer;
    StreamReader reader;

    List<int> Allmytxt = new List<int>();
    // Use this for initialization
    void Start()
    {
        instance = this;
        FileInfo file = new FileInfo(Application.dataPath + "/mytxt.txt");
        if (file.Exists)
        {
            file.Delete();
            file.Refresh();
        }
        else
        {
            file.CreateText();
        }
    }
    //把所有的数据写入文本中
    public void WriteIntoTxt(string message)
    {
        FileInfo file = new FileInfo(Application.dataPath + "/mytxt.txt");
        if (!file.Exists)
        {
            writer = file.CreateText();
        }
        else
        {
            writer = file.AppendText();
        }
        writer.WriteLine(message);
        writer.Flush();
        writer.Dispose();
        writer.Close();
    }
    //读取分数 存储到列表中
    public void ReadOutTxt()
    {
        Allmytxt.Clear();
        reader = new StreamReader(Application.dataPath + "/mytxt.txt", Encoding.UTF8);
        string text;
        while ((text = reader.ReadLine()) != null)
        {
            Allmytxt.Add(int.Parse(text));
        }
        reader.Dispose();
        reader.Close();
    }
    /// <summary>
    /// 获取从列表读取数据之后的List
    /// </summary>
    /// <returns></returns>
    public List<int> GetmytxtList()
    {
        ReadOutTxt();
        return Allmytxt;
    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值