using UnityEngine;
using System.Collections.Generic;
using UnityEngine.UI;
using System.IO;
using System.Collections;
using System.Text;
public class Copy : MonoBehaviour
{
public Text text;
public Text text2;
//www 加载使用流路径
public string streamingAssetsPathWWW;
void Start()
{
#if UNITY_ANDROID && !UNITY_EDITOR
streamingAssetsPathWWW = Application.streamingAssetsPath + "/" ;
#endif
#if UNITY_STANDALONE_WIN || UNITY_EDITOR
streamingAssetsPathWWW = "file://" + Application.streamingAssetsPath + "/" ;
#endif
//1 widonws 成功 安卓读取失败 File.ReadAllText 流路径不能使用
//2 Fill 类读取 不需要 "file://"
// Read(Application.streamingAssetsPath + "/" + "haveLoginUser.json");
//1 widonws 成功 安卓读取失败 FileStream 流路径不能使用
//2 FileStream 类读取 不需要 "file://"
// ReadStream(Application.streamingAssetsPath + "/" + "haveLoginUser.json");
//10 秒后 www 读取 Fill
StartCoroutine(CopyFile("haveLoginUser.json"));
}
IEnumerator CopyFile(string path)
{
yield return new WaitForSeconds(5);
string fullPath = streamingAssetsPathWWW + path;
WWW www = new WWW(fullPath);
yield return www;
if (www.error != null)
{
Debug.Log("加载错误" + www.error);
yield break;
}
if (www.isDone)
{
//Debug.Log("字节流大小:......" + www.bytes.Length);
string fullPathTo = Application.persistentDataPath + "/" + path;
//复制
CreatFile(fullPathTo, www.bytes);
//沙盒路劲读取 File.ReadAllText
Read(fullPathTo);
//沙盒路劲写入 File.WriteAllText
Wirte(fullPathTo, "File.WriteAllText 沙盒路劲写入");
//沙盒路劲写入 FileStream
WriteStream(fullPathTo, "使用FileStream 沙盒路劲写入 ");
//沙盒路劲读取 ReadStream
ReadStream(fullPathTo);
}
}
public void CreatFile(string filePath, byte[] bytes)
{
if (File.Exists(filePath))
{
Debug.Log("文件存在 删除后拷贝......" + filePath);
File.Delete(filePath);
WriteBytes(filePath, bytes);
}
else
{
Debug.Log("拷贝......" + filePath);
WriteBytes(filePath, bytes);
}
}
public void WriteBytes(string filePath, byte[] bytes)
{
FileInfo file = new FileInfo(filePath);
FileStream stream = file.Create();
stream.Write(bytes, 0, bytes.Length);
stream.Close();
stream.Dispose();
}
public void Wirte(string fullPathTo, string str)
{
File.WriteAllText(fullPathTo, str);
}
public void WriteStream(string path,string str )
{
FileStream fs = new FileStream(path, FileMode.Append);
//将字符的byte数组长度赋值给data
byte[] data = Encoding.UTF8.GetBytes(str);
fs.Write(data, 0, data.Length);
fs.Close();
}
public void Read(string fullPathTo)
{
string content = File.ReadAllText(fullPathTo);
text.text = content;
}
public void ReadStream(string path)
{
FileStream fs = new FileStream(path, FileMode.Open);
//获取FileStream的长度,开辟对应的byte内存空间
byte[] data = new byte[fs.Length];
int count = fs.Read(data, 0, data.Length);
string content = Encoding.UTF8.GetString(data, 0, count);
fs.Close();
text2.text = content;
}
}
总结
流路径:读取
|
序号 |
API |
windows |
Ios |
Anroid |
|
1 |
File.ReadAllText FileStream fs fs.Read(data, 0, data.Length); |
支持: Application.streamingAssetsPath |
支持: Application.streamingAssetsPath |
不支持 |
|
2 |
www |
支持: "file://" + Application.streamingAssetsPath + "/xx.txt" |
支持: "file://" + Application.streamingAssetsPath + "/xx.txt" |
支持: Application.streamingAssetsPath + "/xx.txt" |
流路径:写入:
|
序号 |
API |
windows |
Ios |
Anroid |
|
1 |
File.WriteAllText FileStream stream stream.Write(bytes, 0, bytes.Length); |
支持: Application.streamingAssetsPath |
支持: Application.streamingAssetsPath |
不支持 |
沙盒路径的读取和写入 使用 File FileStream WWW 都是可以的
本文介绍了在Unity中进行文件读写操作的不同方法,包括File类、FileStream和WWW的使用。详细对比了在Windows、iOS和Android平台上各种方法的适用性和效果,特别强调了在Android上使用流路径的限制。同时,展示了如何在沙盒路径下进行读写操作。代码示例中包含了读取StreamingAssets和PersistentDataPath的文件,并在不同路径下进行复制、写入和读取的完整流程。

2430

被折叠的 条评论
为什么被折叠?



