Unity3d 读写txt

4 篇文章 0 订阅
using UnityEngine;
using System.Collections;
using System.IO;
using System;

public class ReadOrSaveFile : MonoBehaviour
{
    //保存信息到文件
    public void SaveContentToFile(string _Path,string _fileName, string _content)
    {
        //string fileName = "Text_" + DateTime.Now.ToString("yyyy_MM_dd_H_m_s") + ".txt"; 时间命名文件名
        string directoryPath = _Path;        
        string filename = _fileName;            
        if (!Directory.Exists(directoryPath))  
        {
            Directory.CreateDirectory(directoryPath);
        }
        StreamWriter sw = new StreamWriter(Path.Combine(directoryPath, _fileName));
        sw.Write(_content);
        sw.Flush(); //清楚缓存内容
        sw.Close();

    }
    //截屏
    public void captureScreenshot(string path)
    {
        string _path = Path.Combine(path, "GameLog_" + DateTime.Now.ToString("yyyy_MM_dd_H_m_s") + ".png");
        Application.CaptureScreenshot(_path);
    }
    //读取文件中信息
    public ArrayList GetContentInFile(string _Path, string _content)
    {
        ArrayList arrayList = new ArrayList();
        StreamReader sr = null;     //使用流的形式读取
        sr = File.OpenText(_Path);
        if (sr != null)
        {
            string str;
            while ((str = sr.ReadLine())!= null)
            {
                arrayList.Add(str);
            }
        }
        sr.Close();     //关闭流
        sr.Dispose();   //销毁流
        return arrayList;

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值