android读写csv文件,职场小白求助Unity项目Android端读取CSV文本问题
该楼层疑似违规已被系统折叠隐藏此楼查看此楼本人最近在项目中遇到一些问题,就是读写CSV格式的文本文档,在pc端读写是没问题的,但是安卓端就读不到了,下附代码,希望能得到大佬们的帮助using System.Collections;using System.Collections.Generic;using System.IO;using System.Text;using UnityEngine.
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
本人最近在项目中遇到一些问题,就是读写CSV格式的文本文档,在pc端读写是没问题的,但是安卓端就读不到了,下附代码,希望能得到大佬们的帮助
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEngine;
public class CheckDataManager : MonoBehaviour {
string _loadPath;
string _savePath;
[SerializeField]
private string _fileName = "UserDataTable";
private const string EXTENSION = ".csv";
private CSVTable _table;
// Bind Component
//不同平台的路径也不相同
void Awake()
{
#if UNITY_EDITOR
Debug.Log("这里是编辑器模式");
_loadPath = Application.streamingAssetsPath + "/";
_savePath = Application.persistentDataPath + "/";
#elif UNITY_IOS
Debug.Log("这里是苹果端");
_loadPath = "file://"+Application.dataPath + "/Raw/";
_savePath = Application.persistentDataPath + "/Raw/";
#elif UNITY_ANDROID && !UNITY_EDITOR
Debug.Log("这里是安卓端");
//_loadPath = "jar:file://" + Application.dataPath + "/!/assets/";
//_savePath = "jar:file://" + Application.persistentDataPath + "/";
//_loadPath = "jar:file://" + Application.dataPath + "!/assets/";
//_savePath = "jar:file://" + Application.persistentDataPath + "/";
_loadPath = "jar:file://" + Application.dataPath + "/!/assets/";
_savePath = Application.persistentDataPath + "/";
#endif
Load();
}
///
/// 加载文件
///
void Load()
{
if (!Directory.Exists(_loadPath))
{
Debug.LogError("The file not be found in this path. path:" + _loadPath);
return;
//yield return "";
}
string fullFileName = _loadPath + _fileName + EXTENSION;
//StreamReader sr;
sr = File.OpenText(fullFileName);
//sr = new StreamReader(fullFileName, Encoding.UTF8);
//string content = sr.ReadToEnd();
//Debug.Log("AAAAAAAAAAAAAAA" + content.Length);
//sr.Close();
//sr.Dispose();
WWW www;
www = new WWW(fullFileName);
while (!http://www.isDone) { }
string content = http://www.text;
_table = CSVTable.CreateTable(_fileName, content);
//添加测试
Test();
}
///
/// 存储文件
///
private void Save()
{
if (_table == null)
{
Debug.LogError("The table is null.");
return;
}
string tableContent = _table.GetContent();
if (!Directory.Exists(_savePath))
{
Debug.Log("未找到路径, 已自动创建");
Directory.CreateDirectory(_savePath);
}
string fullFileName = _savePath + _fileName + EXTENSION;
//string fullFileName = _loadPath + _fileName + EXTENSION;
StreamWriter sw;
//sw = File.CreateText(fullFileName);
sw = new StreamWriter(fullFileName, false, Encoding.UTF8);
sw.Write(tableContent);
sw.Close();
sw.Dispose();
/******************************/
if (!Directory.Exists(_savePath))
{
Debug.LogError("The file not be found in this path. path:" + _loadPath);
return;
}
StreamReader sr;
//sr = File.OpenText(fullFileName);
sr = new StreamReader(fullFileName, Encoding.UTF8);
string content = sr.ReadToEnd();
Debug.Log("BBBBBBBBBBBBB" + content.Length);
sr.Close();
sr.Dispose();
/************************************/
_table = null;
}
///
/// 测试方法
///
private void Test()
{
Debug.Log(_table.ToString());
// 修改某一数据
//_table["1"]["Age"] = "10000";
if (!System.IO.File.Exists(_savePath + _fileName + EXTENSION))
{
print("不存在该文件");
Save();
}
else
{
print("已存在该文件");
}
//Save();
}
}
更多推荐
所有评论(0)