android 写入csv 乱码问题,职场小白求助Unity项目Android端读取CSV文本问题

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

本人最近在项目中遇到一些问题,就是读写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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值