关于Application.persistentDataPath在安卓路径的坑

20 篇文章 0 订阅

1.首先Application.persistentDataPath路径在安卓加载的文件是些能被修改的文件才放在这里。
2.Application.persistentDataPath+“/”+“文件名称+后缀“

加载文件

那我们加载安卓Application.persistentDataPath路径下的文件如何加载呢?
1.一般如果是些数据文件首次加载都是放在Application.streamingAsset路径下的
//加载文件时判断文件是否存在

 if (!File.Exists(path))                            //判断文件是否存在
 {
   //那就从Application.streamingAsset路径复制到Application.persistentDataPath路径下。
  //将文件从Application.streamingAsset路径复制到Application.persistentDataPath的方法如下:
  CopyFormStreamassetToPersistent(fileName);
 }
 //文件存在了就直接读取数据了
  string data = File.ReadAllText(path);

//将文件从Application.streamingAsset路径复制到Application.persistentDataPath的方法如下

public void CopyFormStreamassetToPersistent(string fileName)
{
   string formPath;
   string toPath;
   formPath = Application.streamingAssetsPath + "/" + fileName;
    toPath=Application.persistentDataPath + "/" + file_name;
   W W W w=new WWW(formPath)
   while(!w.isDone){}
    if(w.error==null)
    {
        File.WriteAllBytes(toPath, w.bytes);
    }
}

2.那么还有些文件是一开始没有的怎么办呢?

 //首先需要还是判断文件是否存在的问题,
 if (File.Exists(path))                            //判断文件是否存在
 {
    string data = File.ReadAllText(path);
 }
 //创建文件
 StreamWriter sw;
 FileInfo t = new FileInfo(path);
 sw = t.CreateText();
 //把你需要的文件数据写入进去
 sw.Write(data.ToString()); 
 sw.Close();
 sw.Dispose();

保存修改文件

1.一般如果是些数据文件首次加载都是放在Application.streamingAsset路径下的

StreamWriter sw;
 FileInfo t = new FileInfo(path);
 if (!File.Exists(path))                            //判断文件是否存在
 {
   //那就从Application.streamingAsset路径复制到Application.persistentDataPath路径下。
  CopyFormStreamassetToPersistent(fileName);
 }

2.重新创建文件并修改保存文件

 //如果是一些文件需要直接添加的数据
            sw=  t.AppendText
  //如果一些文件需要修改文件的我自己的做法是直接删除
            File.Delete(path);
            sw = t.CreateText();

sw.Write(data.ToString());//这个数据自己添加
 sw.Close();
 sw.Dispose();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值