• UNITY3D讀寫CVS格式文件錄製與存儲數據手套數據

    2019/11/12      點(diǎn)擊:
    說(shuō)明:

    1.寫入一個單元(yuán)格(gé)時,如果含有逗號,則(zé)需要將整個字段用雙引號括(kuò)起來;如果裏麵還有雙引號就替換成兩個雙引號。

    2.寫(xiě)入一行時,末尾要加上\r\n作(zuò)為行分隔符。

    3.讀(dú)取時,也要根(gēn)據(jù)上麵的寫入規則進行解析。

    直接看代碼:

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Text;public class CSVTool {    private static char _csvSeparator = ',';
        private static bool _trimColumns = false;    //獲(huò)取一個單元格的寫入格式
        public static string GetCSVFormat(string str)
        {
            string tempStr = str;
            if (str.Contains(","))
            {
                if (str.Contains("\""))
                {
                    tempStr = str.Replace("\"", "\"\"");
                }
                tempStr = "\"" + tempStr + "\"";
            }
            return tempStr;
        }    //獲取一行的(de)寫入格(gé)式
        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;
        }    //解析一行
        public static ListParseLine(string line)
        {
            StringBuilder _columnBuilder = new StringBuilder();
            ListFields = new List();
            bool inColumn = false;  //是否是在一個列元素裏(lǐ)
            bool inQuotes = false;  //是否(fǒu)需要轉義
            bool isNotEnd = false;  //讀取完(wán)畢未結束轉義
            _columnBuilder.Remove(0, _columnBuilder.Length);
            //空行也是一個空元素,一個逗號是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)//這個字符已經結束了整行
                    {
                        if (character == '"') //正常轉義結束,且該行已經結束
                        {
                            inQuotes = false;
                            continue;     //當前(qián)字符不用添加(jiā),跳出後直結束後會添加該元素
                        }
                        else //異常結束,轉義未收尾
                        {
                            isNotEnd = true;
                        }
                    }
                    else if (character == '"' && line[i + 1] == _csvSeparator) //結(jié)束轉義,且(qiě)後麵有可能還有數據
                    {
                        inQuotes = false;
                        inColumn = false;
                        i++; //跳過(guò)下一個字符
                    }
                    else if (character == '"' && line[i + 1] == '"') //雙引號(hào)轉義
                    {
                        i++; //跳過下一個(gè)字符
                    }
                    else if (character == '"') //雙引號單獨出(chū)現(這種情(qíng)況(kuàng)實際上已經是格式錯誤,為(wéi)了兼容可暫(zàn)時不(bú)處理)
                    {
                        throw new Exception("格式錯誤,錯誤的(de)雙引號轉義");
                    }
                    //其(qí)他情況直接跳出,後麵正常添加
                }
                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,並且不處理當前字(zì)符,直(zhí)接進行Add
                {
                    Fields.Add(_trimColumns ? _columnBuilder.ToString().Trim() : _columnBuilder.ToString());
                    _columnBuilder.Remove(0, _columnBuilder.Length);
                }
                else // append the current column
                    _columnBuilder.Append(character);
            }
            //(標準格式一行結尾不需要逗號結尾(wěi),而上麵(miàn)for是遇(yù)到逗(dòu)號才添(tiān)加的,為了兼容還要添(tiān)加一次)
            if (inColumn)
            {
                if (isNotEnd)
                {
                    _columnBuilder.Append("\r\n");
                }
                Fields.Add(_trimColumns ? _columnBuilder.ToString().Trim() : _columnBuilder.ToString());
            }
            else  //如果inColumn為false,說明已經添加,一個字符為分隔符,所以後(hòu)麵要加上一(yī)個空元素
            {
                Fields.Add("");
            }
            return Fields;
        }
        //讀取文件
        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);
        }    //打印
        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]);
                }
            }
        }
    }

    關於UNITY的路(lù)徑有4個類型:

    Application.dataPath:該路徑指向我們Unity編輯器的Asset文(wén)件夾

    Application.persistentDataPath:該路徑指向ioses和androids的沙盒路徑

    Application.streamingAssetsPath:streamingAsset文件夾路徑,在任何平台都可以通過(guò)這個路徑讀取到文件夾裏的內容

    Application.temporaryCachePath:臨時數據文件路徑


    自愉色色,亚洲色孩,jk无遮拦,日日夜夜7799天天综合,999夜夜,7799天天综合天天综合网精品视频,日日夜夜精品视频7799,天天综合7799精品影视