CSV 读写

using System;
using System.IO;
using System.Runtime.InteropServices;
using UnityEngine;

public class CSVReader : IDisposable
{
    private bool m_disposed;
    private StreamReader m_file;
    private int m_lineIndex = -1;
    private bool m_silent;
    private int m_tokenIndex;
    private string[] m_tokens;
    private static char[] separators = new char[] { ',' };
    private static char[] subSeparators = new char[] { '+' };
    private static char[] trimCharacters = new char[] { ' ', '"' };

    public CSVReader(string fileName, bool silent = false)
    {
        this.m_silent = silent;
        try
        {
            this.m_file = new StreamReader(fileName);
            this.m_file.ReadLine();
        }
        catch (Exception exception)
        {
            if (!this.m_silent)
            {
                Debug.LogWarning("Could not load: " + fileName + ", error: " + exception.Message);
            }
        }
    }

    public void Dispose()
    {
        this.Dispose(true);
        GC.SuppressFinalize(this);
    }

    protected virtual void Dispose(bool disposing)
    {
        if (!this.m_disposed)
        {
            if (disposing && (this.m_file != null))
            {
                this.m_file.Dispose();
            }
            this.m_disposed = true;
        }
    }

    ~CSVReader()
    {
        this.Dispose(false);
    }

    public bool NextRow()
    {
        if (this.m_file == null)
        {
            return false;
        }
        string str = this.m_file.ReadLine();
        if (str == null)
        {
            return false;
        }
        this.m_tokens = str.Split(separators);
        this.m_tokenIndex = 0;
        this.m_lineIndex++;
        return true;
    }

    private string NextToken()
    {
        return this.m_tokens[this.m_tokenIndex++].Trim(trimCharacters);
    }

    public T ReadEnum<T>(T defaultValue)
    {
        string str = this.ReadString();
        if (!string.IsNullOrEmpty(str))
        {
            try
            {
                return (T) Enum.Parse(typeof(T), str, true);
            }
            catch (Exception)
            {
            }
        }
        return defaultValue;
    }

    public float ReadFloat()
    {
        if (this.m_tokenIndex < this.m_tokens.Length)
        {
            string s = this.NextToken();
            float result = 0f;
            if (!float.TryParse(s, out result) && !this.m_silent)
            {
                Debug.LogError(string.Format("Could not parse float on line {0}, token {1}: {2}", this.m_lineIndex + 1, this.m_tokenIndex + 1, s));
            }
            return result;
        }
        if (!this.m_silent)
        {
            Debug.LogError(string.Format("Out of tokens on line {0}, requested token at index {1}", this.m_lineIndex + 1, this.m_tokenIndex + 1));
        }
        return 0f;
    }

    public int ReadInt()
    {
        if (this.m_tokenIndex < this.m_tokens.Length)
        {
            string s = this.NextToken();
            int result = 0;
            if (!int.TryParse(s, out result) && !this.m_silent)
            {
                Debug.LogError(string.Format("Could not parse int on line {0}, token {1}: {2}", this.m_lineIndex + 1, this.m_tokenIndex + 1, s));
            }
            return result;
        }
        if (!this.m_silent)
        {
            Debug.LogError(string.Format("Out of tokens on line {0}, requested token at index {1}", this.m_lineIndex + 1, this.m_tokenIndex + 1));
        }
        return 0;
    }

    public string ReadString()
    {
        if (this.m_tokenIndex < this.m_tokens.Length)
        {
            return this.NextToken();
        }
        if (!this.m_silent)
        {
            Debug.LogError(string.Format("Out of tokens on line {0}, requested token at index {1}", this.m_lineIndex + 1, this.m_tokenIndex + 1));
        }
        return string.Empty;
    }

    public string[] ReadStringArray()
    {
        if (this.m_tokenIndex < this.m_tokens.Length)
        {
            string[] strArray = this.m_tokens[this.m_tokenIndex++].Trim(trimCharacters).Split(subSeparators);
            if ((strArray.Length == 1) && string.IsNullOrEmpty(strArray[0]))
            {
                return new string[0];
            }
            return strArray;
        }
        if (!this.m_silent)
        {
            Debug.LogError(string.Format("Out of tokens on line {0}, requested token at index {1}", this.m_lineIndex + 1, this.m_tokenIndex + 1));
        }
        return new string[0];
    }
}
using System;
using System.IO;
using UnityEngine;

public class CSVWriter : IDisposable
{
    private bool m_disposed;
    private StreamWriter m_file;
    private bool m_startOfLine = true;
    private static char separator = ',';
    private static char subSeparator = '+';

    public CSVWriter(string fileName, params string[] columns)
    {
        try
        {
            this.m_file = new StreamWriter(fileName);
            foreach (string str in columns)
            {
                this.WriteString(str);
            }
            this.EndRow();
        }
        catch (Exception exception)
        {
            Debug.LogError("Could not open: " + fileName + ", error: " + exception.Message);
        }
    }

    public void Dispose()
    {
        this.Dispose(true);
        GC.SuppressFinalize(this);
    }

    protected virtual void Dispose(bool disposing)
    {
        if (!this.m_disposed)
        {
            if (disposing && (this.m_file != null))
            {
                this.m_file.Dispose();
            }
            this.m_disposed = true;
        }
    }

    public void EndRow()
    {
        if (this.m_file != null)
        {
            this.m_file.Write('
');
            this.m_startOfLine = true;
        }
    }

    ~CSVWriter()
    {
        this.Dispose(false);
    }

    public void WriteFloat(float val)
    {
        if (!this.m_startOfLine)
        {
            this.m_file.Write(separator);
        }
        this.m_startOfLine = false;
        this.m_file.Write(val);
    }

    public void WriteInt(int val)
    {
        if (!this.m_startOfLine)
        {
            this.m_file.Write(separator);
        }
        this.m_startOfLine = false;
        this.m_file.Write(val);
    }

    public void WriteString(string val)
    {
        if (!this.m_startOfLine)
        {
            this.m_file.Write(separator);
        }
        this.m_startOfLine = false;
        this.m_file.Write(val);
    }

    public void WriteStringArray(string[] vals)
    {
        if (!this.m_startOfLine)
        {
            this.m_file.Write(separator);
        }
        this.m_startOfLine = false;
        bool flag = true;
        foreach (string str in vals)
        {
            if (!flag)
            {
                this.m_file.Write(subSeparator);
            }
            flag = false;
            this.m_file.Write(str);
        }
    }
}
原文地址:https://www.cnblogs.com/123ing/p/4042913.html