项目中的需求,读取表格中的数据,组拼成数组。如果有参考的朋友,要根据自己的项目需求进行修改方法。
private void ReadData()
{
String path = Application.streamingAssetsPath + "/data.csv";
if (!File.Exists(path))
{
Debug.LogError("文件不存在");
return;
}
string[] datas = File.ReadAllLines(path);
List<string> listResult = new List<string>();
for (int i = 0; i < datas.Length; i++)
{
if (datas[i].Length == 0) continue;
string[] result = datas[i].Split('\t');
for (int j = 0; j < result.Length; j++)
{
if (result[j].Length == 0) continue;
listResult.Add(result[j].Trim());
}
}
if (listResult.Count % 6 != 0)
{
Debug.LogError("数据输入有误,数据个数为:"+listResult.Count);
return;
}
string txtRs = "";
for (int i = 0; i < listResult.Count; )
{
txtRs += "datas[5] = new[] {"+listResult[i]+"f,";
txtRs += listResult[i+1]+"f,";
txtRs += listResult[i+2]+"f,";
txtRs += listResult[i+3]+"f,";
txtRs += listResult[i+4]+"f,";
txtRs += listResult[i+5]+"f};"+'\n';
i += 6;
}
String txtPath = Application.streamingAssetsPath + "/rst.txt";
if (!System.IO.File.Exists(path))
{
FileStream stream = System.IO.File.Create(path);
stream.Close();
stream.Dispose();
}
using (StreamWriter writer = new StreamWriter(txtPath, true))
{
writer.WriteLine(txtRs);
}
Debug.Log(txtRs);
Debug.Log("数据写入完成");
}