Unity自动创建Txt和读取TXT 往Txt里面可以写入 两种创建方法和四种读取方式

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;  //操作文件夹时需引用该命名空间
using System.Text;

public class TxtWriteAndRead : MonoBehaviour
{
    void Start()
    {
       //  AddTxtTextByFileStream("第一种方法添加text文本");

       AddTxtTextByFileInfo("第二种方法添加txt文本");

      //重写数据
      //   ReWriteMyTxtByFileStreamTxt();

         ReadTxtSecond();
        //ReadTxtThird();
        //ReadTxtForth();
        //ReadTxtFifth();
    }

    /// <summary>
    /// 创建txt 方法一
    /// </summary>
    /// <param name="txtText"></param>
    public void AddTxtTextByFileStream(string txtText)
    {
        string path = Application.streamingAssetsPath + "/MyTxtByFileStream.txt";
        // 文件流创建一个文本文件
        FileStream file = new FileStream(path, FileMode.Create);
        //得到字符串的UTF8 数据流
        byte[] bts = System.Text.Encoding.UTF8.GetBytes(txtText);
        // 文件写入数据流
        file.Write(bts, 0, bts.Length);
        if (file != null)
        {
            //清空缓存
            file.Flush();
            // 关闭流
            file.Close();
            //销毁资源
            file.Dispose();
        }
    }

    /// <summary>
    /// 可以重新写入文件到一个已存在的txt文本中去
    /// </summary>
    void ReWriteMyTxtByFileStreamTxt()
    {
        string path = Application.streamingAssetsPath + "/MyTxtByFileStream.txt";

        string[] strs = { "123", "321", "234" };

        File.WriteAllLines(path, strs);

        /*
         文本内容为:(逐行显示的)
         123
         321
         234
         原文本内容为:
         第一种方法添加text文本
         */

        //如果想在某一行中添加一行新的,可以将原txt文本读取下来,保存到数组里,
        //然后新建一个数组,载将原数组文本与新加入的行数据同时写入到新数组里
        //然后用新数组数据替换(重写)原来的数据
    }

    /// <summary>
    /// 创建txt 方法二
    /// </summary>
    /// <param name="txtText"></param>
    public void AddTxtTextByFileInfo(string txtText)
    {
        string path = Application.streamingAssetsPath + "/MyTxtByFileStream1.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();
    }

  

    /// <summary>
    /// 读取txt文本方法二
    /// 逐行读取 该方法可以单独获取某一行的数据
    /// </summary>
    void ReadTxtSecond()
    {
        string path = Application.streamingAssetsPath + "/MyTxtByFileStream.txt";
        //逐行读取返回的为数组数据
        string[] strs = File.ReadAllLines(path);

        foreach (string item in strs)
        {
            print(item); //第二种方法添加txt文本
                         //只有这种方法可以读取全你的Txt其他的都只能读取一行  
        }
    }

    /// <summary>
    /// 第三种读取方法
    /// 需引用  using System.Text;
    /// </summary>
    void ReadTxtThird()
    {

        string path = Application.streamingAssetsPath + "/MyTxtByFileStream.txt";

        string str = File.ReadAllText(path, Encoding.UTF8);

        print(str); //第二种方法添加txt文本
                    //第二种方法添加txt文本
    }

    /// <summary>
    /// 第四种读取方法
    /// </summary>
    void ReadTxtForth()
    {
        string path = Application.streamingAssetsPath + "/MyTxtByFileStream.txt";
        //FileStream fsSource = new FileStream(path, FileMode.Open, FileAccess.Read);

        //文件读写流
        StreamReader strr = new StreamReader(path);
        //读取内容
        string str = strr.ReadToEnd();

        print(str);  //第二种方法添加txt文本
                     //第二种方法添加txt文本

    }

    /// <summary>
    /// 第五种读取方法
    /// 文件流方式
    /// </summary>
    void ReadTxtFifth()
    {
        string path =  Application.streamingAssetsPath + "/MyTxtByFileStream.txt";
        FileStream files = new FileStream(path, FileMode.Open, FileAccess.Read);
        byte[] bytes = new byte[files.Length];
        files.Read(bytes, 0, bytes.Length);
        files.Close();
        string str = UTF8Encoding.UTF8.GetString(bytes);
        print(str);     //第二种方法添加txt文本
                        //第二种方法添加txt文本
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值