向手机缓存区写入Text

1.PC端读取Resources文件夹下的.txt文件,并解析分为一行一行存入ArrayList
2.Android端如果文件不存在则在缓存区创建一个.txt,并将Resources文件夹下.test.txt文件信息复制写入新创建的.txt中

 /// <summary>
    /// 获取当前平台路径
    /// </summary>
    void GetPlatformPath()
    {
        if(Application .platform ==RuntimePlatform.Android )
        {
            PathSource = Application.persistentDataPath;
           //test.txt,文件流写入和创建需要文件后缀名
        }
        else if(Application.platform==RuntimePlatform.WindowsEditor||Application .platform ==RuntimePlatform.WindowsPlayer  )
        {
            PathSource = Application.dataPath;
            //testpc端Resources读取不需要后缀名       
        }
    }

//android端方法

 ArrayList FunAndroid()
    {
        StreamWriter wt;
        FileInfo t = new FileInfo(PathSource + "//" + ConfigPath);
        if (!t.Exists)
        {
            wt = t.CreateText();
            TextAsset te = Resources.Load(ConfigPathName) as TextAsset;
            string strs = Encoding.UTF8.GetString(te.bytes);
            if (te != null)
            {
                wt.Write(strs, 0, 1024);
                wt.Close();
                wt.Dispose();
            }
            Debug.Log("-----------dont have ,creat one");
        }
        else
        {
            StreamReader sr = null;
            try
            {
                sr = File.OpenText(PathSource + "//" + ConfigPath);
            }
            catch (System.Exception e)
            {
                return null;
            }
            string line;
            ArrayList arrlist = new ArrayList();
            while ((line = sr.ReadLine()) != null)
            {
                arrlist.Add(line);
            }
            sr.Close();
            sr.Dispose();
            return arrlist;
        }
        return null;
    }

//pc端方法

 ArrayList FunPc()
    {
        TextAsset te = Resources.Load(ConfigPathName) as TextAsset;
        if (te != null)
        {
            string tempStr = te.text;
            string[] Strs = tempStr.Split('\n');
            if (Strs.Length > 0)
            {
                ArrayList arrlist = new ArrayList();
                for (int i = 0; i < Strs.Length; i++)
                {
                    arrlist.Add(Strs[i]);
                }
                return arrlist;
            }
            return null;
        }
        else
        {
            return null;
        }
    }

//联合

 ArrayList  GetFile()
    {
        if(Application .platform ==RuntimePlatform.Android )
        {
            FunAndroid();
        }
        else if (Application.platform == RuntimePlatform.WindowsEditor || Application.platform == RuntimePlatform.WindowsPlayer)
        {
            FunPc();
        }
        return null;
    }
 //临时给.txt写入信息
    public void Test()
    {
        StreamWriter wt;
        //在文件后面添加写入信息
        FileInfo fi = new FileInfo(PathSource + "//" + ConfigPath);
        wt = fi.AppendText();//如果直接重新写入,不需要在原文件后面追加内容则用fi.CreateText ();  
        string strs = "\n"+"ab,25,100";
        wt.WriteLine(strs);
        wt.Close();
        wt.Dispose();
        Debug.Log("-----------add -------------");
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值