unity中保存为和加载.cvs格式的文件

有些文件需要用表格形式存储,方便修改,由此我写了按.csv格式存储和加载文件的代码,方便大家参考。
加载.csv格式的文件

    public static List<List<string>> loadInformation(string fullPath)
    {
        List<List<string>> informationList = new List<List<string>>();
        if (File.Exists(fullPath))//判断文件是否存在
        {
            StreamReader streamReader = new StreamReader(fullPath);//读取文件
            string information = streamReader.ReadToEnd();//读取文件中的数据
            string[] strArr = information.Split( '\n' );//按每一行读取相应的内容
            for (int i = 1; i < strArr.Length - 1; i++)
            {
                List<string> singleInformation = new List<string>();
                string[] arr = strArr[i].Split(',');//将每一行的信息按英文,划分
                for (int j = 0; j < arr.Length; j++)
                    singleInformation.Add(arr[j]);
                informationList.Add(singleInformation);
            }
            streamReader.Close();//关闭文件
        }
        else informationList = new List<List<string>> ();
        return informationList;//返回的数据类型
    }

保存为.csv格式的文件

    public static void Save(string filePath, string fileName, string informationType, List<List<string>> informationList)
    {
        string FilePath = filePath + '/' + fileName + ".csv";
        FileInfo file = new FileInfo(FilePath);
        StreamWriter streamWriter = file.CreateText();//按文件路径和文件名创建对应的文件
        if (File.Exists(FilePath))
        {
            streamWriter.WriteLine(informationType);
            for (int i = 0; i < informationList.Count; i++)
            {
                for (int j = 0; j < informationList[i].Count; j++)
                {
                    string singleInformation = " ";
                    if (j != informationList[i].Count - 1)
                        singleInformation = informationList[i][j] + ",";//将每一行内容写入文件
                    else
                        singleInformation = informationList[i][j] + "\n";//写入最后一个内容并换行
                    streamWriter.Write(singleInformation);
                }
            }
            streamWriter.Close();//关闭文件
            streamWriter.Dispose();
        }
    }
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夏天里的草

你的鼓励是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值