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();