读取和保存txt文件

涉及知识点

  1. 读取文件内容;
  2. 创建文件夹和文件,并写入内容;
  3. 以覆盖的方式写入内容,以添加的方式写入内容;

读取和保存txt文件源码

using UnityEngine;

using System.Text.RegularExpressions;
using System;
using System.Net;
using System.Text;
using System.Threading;
using System.IO;
using UnityEngine.UI;


public class ReadAndSaveText : MonoBehaviour
{
    public InputField input;
    public Toggle isAdd;
    public Text showSaveData;
    public Text showReadData;


    private WebClient myWebClient = new WebClient();
    private string url = "http://www.163.com";
    private string userDocPath;
    private string directory;
    private string filePath;    


    protected void Start()
    {
#if UNITY_STANDALONE_LINUX
        userDocPath = Environment.GetEnvironmentVariable("HOME");//home目录
#elif UNITY_STANDALONE_WIN
        userDocPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);//文档目录
#endif    
        directory = userDocPath + "/.Test";
        filePath = directory + "/Test.txt";
        Debug.Log("Save data directory: " + directory);  
    }


    public void ReadData()
    {
        string readData = null;
        if (Directory.Exists(directory) && File.Exists(filePath))
        {
            using (StreamReader read = new StreamReader(filePath, Encoding.Default))
            {
                readData = read.ReadToEnd();
            }            
        }


        showReadData.text = "读取内容:\n" + readData;
    }


    public void SaveData()
    {
        string data = input.text;    


        Debug.Log("Saving data file.");


        if (!Directory.Exists(directory))
            Directory.CreateDirectory(directory);


        if (!File.Exists(filePath))
        {
            Debug.Log("Creating new data file.");


            //创建文件并写入内容,同时换行
            FileStream fs = new FileStream(filePath, FileMode.Create);
            StreamWriter sw = new StreamWriter(fs);
            sw.WriteLine(data);


            sw.Flush();
            sw.Close();
            fs.Close();


            showSaveData.text += "\n" + data;
        }
        else
        {
            if (!isAdd.isOn)
            {
                //覆盖内容,同时换行
                using (StreamWriter sw = new StreamWriter(filePath))
                {
                    sw.WriteLine(data);
                }


                showSaveData.text = "保存内容:\n" + data;
            }
            else
            {
                //在已有的文本后添加新的内容,同时换行
                using (StreamWriter sw = File.AppendText(filePath))
                {
                    sw.WriteLine(data);
                }


                showSaveData.text += "\n" + data;
            }
        }


        Debug.Log("Save data success.");
    }   

}

文章仅供学习和参考!如有错误请指正!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值