樱花草在线播放免费高清观看,国产成人久久综合777777麻豆,啦啦啦在线视频免费观看正在播放1,在线天堂√中文,亚洲色大成网站www永久男同,国产丰满乱子伦无码专区,日韩精品 在线 国产 丝袜 ,三级在线看中文字幕完整版
  • 您的位置:首頁 > 新聞動態 > Unity3D

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

    2019/11/12??????點擊:
    說明:

    1.寫入一個單元格時,如果含有逗號,則需要將整個字段用雙引號括起來;如果里面還有雙引號就替換成兩個雙引號。

    2.寫入一行時,末尾要加上\r\n作為行分隔符。

    3.讀取時,也要根據上面的寫入規則進行解析。

    直接看代碼:

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Text;
    
    public class CSVTool {
    
        private static char _csvSeparator = ',';
        private static bool _trimColumns = false;
    
        //獲取一個單元格的寫入格式
        public static string GetCSVFormat(string str)
        {
            string tempStr = str;
            if (str.Contains(","))
            {
                if (str.Contains("\""))
                {
                    tempStr = str.Replace("\"", "\"\"");
                }
                tempStr = "\"" + tempStr + "\"";
            }
            return tempStr;
        }
    
        //獲取一行的寫入格式
        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;  //是否是在一個列元素里
            bool inQuotes = false;  //是否需要轉義
            bool isNotEnd = false;  //讀取完畢未結束轉義
            _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;     //當前字符不用添加,跳出后直結束后會添加該元素
                        }
                        else //異常結束,轉義未收尾
                        {
                            isNotEnd = true;
                        }
                    }
                    else if (character == '"' && line[i + 1] == _csvSeparator) //結束轉義,且后面有可能還有數據
                    {
                        inQuotes = false;
                        inColumn = false;
                        i++; //跳過下一個字符
                    }
                    else if (character == '"' && line[i + 1] == '"') //雙引號轉義
                    {
                        i++; //跳過下一個字符
                    }
                    else if (character == '"') //雙引號單獨出現(這種情況實際上已經是格式錯誤,為了兼容可暫時不處理)
                    {
                        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是遇到逗號才添加的,為了兼容還要添加一次)
            if (inColumn)
            {
                if (isNotEnd)
                {
                    _columnBuilder.Append("\r\n");
                }
                Fields.Add(_trimColumns ? _columnBuilder.ToString().Trim() : _columnBuilder.ToString());
            }
            else  //如果inColumn為false,說明已經添加,一個字符為分隔符,所以后面要加上一個空元素
            {
                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的路徑有4個類型:

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

    Application.persistentDataPath:該路徑指向iOS和Android的沙盒路徑

    Application.streamingAssetsPath:streamingAsset文件夾路徑,在任何平臺都可以通過這個路徑讀取到文件夾里的內容

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


    主站蜘蛛池模板: 人人妻人人澡人人爽| 女人毛片免费观看| 欧美激情 亚洲 在线| 制服丝袜美腿一区二区| 无码aⅴ精品一区二区三区浪潮| 精品香蕉久久久午夜福利| 久久夜色精品国产噜噜av| 欧美猛男军警gay自慰| 韩国午夜理论在线观看| 欧美美女人体艺术| 中文无码熟妇人妻av在线| 四虎永久在线精品免费一区二区| 久久午夜无码鲁丝片直播午夜精品| www.夜夜操.com| 国产无遮挡吃胸膜奶免费看| 精品久久久久久无码国产| 成人h动漫精品一区二区樱花动漫 欧美性暴力变态xxxx | 两个人看的www免费视频中文| 丰满的妽妽用身体满足了我电影| 亚洲精品无码永久在线观看性色| gogogo香港高清免费完整版| 野花高清在线观看免费全集7| 久久成人免费精品网站| 久久天天躁狠狠躁夜夜av浪潮 | 国产成人亚洲综合色就色| 国产亚av手机在线观看| 亚洲欧洲无码av不卡在线| 欧美另类xx肥妇| 天天看片视频免费观看| 国产无吗一区二区三区在线欢| 成人伊人精品色xxxx视频| 亚洲欧洲自拍拍偷精品 美利坚| 国产无吗一区二区三区在线欢| 无码专区国产精品视频| 色诱久久久久综合网ywww| 三年高清在线观看全集下载| 一区二区精品视频日本| 国产一三四2021不卡 | 国产三级a在线观看| 内射人妻骚骚骚| 香蕉伊蕉伊中文视频在线|