UNITY3D读写CVS格式文件录制与存储数据手套数据
2019/11/12 点击(jī):
说明:
1.写入一个单元格时,如果含有逗号,则需要将(jiāng)整个字段用双引号(hào)括(kuò)起来(lái);如果里面还有(yǒu)双引号就替换成两个(gè)双(shuāng)引号。
2.写入一(yī)行时,末尾要加上\r\n作为行分隔符。3.读取时,也(yě)要根据上面的写入规则进行解析。
直接看(kàn)代码:
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;public class CSVTool { private static char _csvSeparator = ',';
private static bool _trimColumns = false; //获取一个单元(yuán)格的写入格式
public static string GetCSVFormat(string str)
{
string tempStr = str;
if (str.Contains(","))
{
if (str.Contains("\""))
{
tempStr = str.Replace("\"", "\"\"");
}
tempStr = "\"" + tempStr + "\"";
}
return tempStr;
} //获取一(yī)行的写入格式
public static string GetCSVFormatLine(ListstrList)
{
string tempStr = "";
for (int i = 0; i < strList.Count - 1; i++)
{
string str = strList[i];
tempStr = tempStr + GetCSVFormat(str) + ",";
}
tempStr = tempStr + GetCSVFormat(strList[strList.Count - 1]) + "\r\n";
return tempStr;
} //解析一行(háng)
public static ListParseLine(string line)
{
StringBuilder _columnBuilder = new StringBuilder();
ListFields = new List();
bool inColumn = false; //是否(fǒu)是在一个列元素里
bool inQuotes = false; //是否需要转义
bool isNotEnd = false; //读取完(wán)毕未结束转义
_columnBuilder.Remove(0, _columnBuilder.Length);
//空行也(yě)是一个空元素,一(yī)个逗号是2个空元素
if (line == "")
{
Fields.Add("");
}
// Iterate through every character in the line
for (int i = 0; i < line.Length; i++)
{
char character = line[i];
// If we are not currently inside a column
if (!inColumn)
{
// If the current character is a double quote then the column value is contained within
// double quotes, otherwise append the next character
inColumn = true;
if (character == '"')
{
inQuotes = true;
continue;
}
}
// If we are in between double quotes
if (inQuotes)
{
if ((i + 1) == line.Length)//这个字符已经结(jié)束了整(zhěng)行
{
if (character == '"') //正常转义结(jié)束,且该(gāi)行已经结束
{
inQuotes = false;
continue; //当前字符不用添加,跳出后直结束后会添加该元素
}
else //异常结束,转义(yì)未收尾(wěi)
{
isNotEnd = true;
}
}
else if (character == '"' && line[i + 1] == _csvSeparator) //结束(shù)转义,且后面有可能还有数据
{
inQuotes = false;
inColumn = false;
i++; //跳过下一个字符
}
else if (character == '"' && line[i + 1] == '"') //双引号转义(yì)
{
i++; //跳过下一个字符
}
else if (character == '"') //双引号单独出现(xiàn)(这种情况(kuàng)实际上已(yǐ)经(jīng)是格式错(cuò)误,为了兼容可暂时不处理)
{
throw new Exception("格式错误,错误的双引号转义");
}
//其他情况直接跳出,后面正常添加
}
else if (character == _csvSeparator)
inColumn = false;
// If we are no longer in the column clear the builder and add the columns to the list
if (!inColumn) //结束该元素时inColumn置为false,并且不处理当前字符,直接进行Add
{
Fields.Add(_trimColumns ? _columnBuilder.ToString().Trim() : _columnBuilder.ToString());
_columnBuilder.Remove(0, _columnBuilder.Length);
}
else // append the current column
_columnBuilder.Append(character);
}
//(标准格式一行结尾不需要逗号结尾,而上面for是遇(yù)到逗号(hào)才添加的,为了兼容还要添加一次)
if (inColumn)
{
if (isNotEnd)
{
_columnBuilder.Append("\r\n");
}
Fields.Add(_trimColumns ? _columnBuilder.ToString().Trim() : _columnBuilder.ToString());
}
else //如果inColumn为false,说明(míng)已经添加,一个(gè)字符为分隔符,所以后面要加上一个空元素
{
Fields.Add("");
}
return Fields;
}
//读(dú)取文件
public static ListRead(string filePath, Encoding encoding)
{
Listresult = new List();
string content = File.ReadAllText(filePath, encoding);
string[] lines = content.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < lines.Length; i++)
{
Listline = ParseLine(lines[i]);
result.Add(line);
}
return result;
}
//写入文件
public static void Write(string filePath, Encoding encoding, Listresult)
{
StringBuilder builder = new StringBuilder();
for (int i = 0; i < result.Count; i++)
{
Listline = result[i];
builder.Append(GetCSVFormatLine(line));
}
File.WriteAllText(filePath, builder.ToString(), encoding);
} //打印(yìn)
public static void Debug(Listresult)
{
for (int i = 0; i < result.Count; i++)
{
Listline = result[i];
for (int j = 0; j < line.Count; j++)
{
UnityEngine.Debug.LogWarning(line[j]);
}
}
}
}
关于(yú)UNITY的路径有4个类型:
Application.dataPath:该路径指向我们Unity编辑器的Asset文件夹
Application.persistentDataPath:该路径(jìng)指向ioses和androids的沙盒路径(jìng)
Application.streamingAssetsPath:streamingAsset文件夹路径,在任何平台都可(kě)以通过这个(gè)路径读取(qǔ)到文件夹里(lǐ)的内容(róng)
Application.temporaryCachePath:临时数据文件路径
- 上一篇:WISEGLOVE数据手套的部(bù)分高校客户 2019/11/29
- 下一篇:unity3d读写EXCEL文件的方法 2019/11/12
