zoukankan      html  css  js  c++  java
  • CSV文件读写

    很多情况下都会用到CSV的文件读写,比如两个系统做数据交换。本文提供了一个完整的CSV读写类,可以方便的进行读写操作。

    CSVHelper

    using System;
     using System.Collections.Generic;
     using System.Text;
     using System.IO;
     
    namespace CSV
     {
      public abstract class CSVHelper
      {
       protected string[] _heads = null;
       protected const int MaxBuffer = 65535;
       protected StringBuilder strB;
       protected string _fileName;
       protected const char _colChar = ',';
       protected const string _rowsChar = "\r\n";
       protected const char _rowsSplit = '\r';
       protected FileInfo _fi = null;
       private object _sync = null;
     
      public CSVHelper(string fileName, string[] heads)
       {
        if (!string.IsNullOrEmpty(fileName))
        {
         strB = new StringBuilder(MaxBuffer);
         _heads = heads;
         _fileName = fileName;
         _sync = new object();
     
        if (heads != null)
         {
          AppendList(heads);
         }
     
        _fi = new FileInfo(fileName);
        }
        else
        {
         throw new ArgumentException("参数错误");
        }
       }
     
      private void AppendList(string[] list)
       {
        lock (_sync)
        {
         int index = 0;
     
        foreach (string head in list)
         {
          strB.Append(head);
     
         index++;
     
         if (index == list.Length)
          {
           strB.Append(_rowsChar);
          }
          else
          {
           strB.Append(_colChar);
          }
         }
     
        //如果大于MaxBuffer 写入磁盘
         if (strB.Length >= MaxBuffer)
         {
          Save();
         }
        }
       }
     
      public void Append(string[] values)
       {
        if (_heads == null || (_heads != null && values.Length == _heads.Length))
        {
         AppendList(values);
        }
        else
        {
         throw new Exception("head的条目数和value的条目数不一样");
        }
       }
     
      public void Save()
       {
        if (!_fi.Exists)
        {
         FileStream fs = _fi.Create();
     
        fs.Close();
     
        fs.Dispose();
        }
     
       using (StreamWriter sw = _fi.AppendText())
        {
         sw.Write(strB.ToString());
     
        sw.Close();
     
        sw.Dispose();
        }
     
       strB.Remove(0, strB.Length);
       }
      }
     }

    CSVReader

    using System;
     using System.Collections.Generic;
     using System.Text;
     using System.IO;
     
    namespace CSV
     {
      public class CSVReader : CSVHelper
      {
       private FileStream _fs;
       private int _curLocation;
       private int _length;
     
      /// <summary>
       /// 
      /// </summary>
       /// <param name="fileName">需要读取的完全路径</param>
       /// <param name="heads">头,可以为null</param>
       public CSVReader(string fileName, string[] heads)
        : base(fileName, heads)
       {
        _curLocation = 0;
        _fs = _fi.OpenRead();
        _length = (int)_fs.Length;
       }
     
      public string[] ReadLine()
       {
        while (true)
        {
         if (_fs.ReadByte() == _rowsSplit)
         {
          int start = _curLocation;
          int end = (int)_fs.Position - 1;
          _fs.Position = _curLocation;
          int bufferLength = end - start;
          byte[] buffer = new byte[bufferLength];
     
         StringBuilder sb = new StringBuilder(bufferLength);
     
         _fs.Read(buffer, 0, end - start);
     
         sb.Append(Encoding.UTF8.GetString(buffer));
     
         _curLocation = end + 2;
          _fs.Position = _curLocation;
     
         if (sb.Length > 0)
          {
           return sb.ToString().Split(_colChar);
          }
         }
         //没找到
         if (_fs.Position >= (_length - 1))
         {
          break;
         }
        }
     
       if (_fs != null)
        {
         _fs.Close();
         _fs.Dispose();
        }
     
       return null;
       }
      }
     }


    CSVWritter

    using System;
     using System.Collections.Generic;
     using System.Text;
     using System.IO;
     
    namespace CSV
     {
      public class CSVWriter : CSVHelper
      {
       /// <summary>
       /// 
      /// </summary>
       /// <param name="fileName">写入的完全路径</param>
       /// <param name="heads">需要写入的头,可以为null</param>
       public CSVWriter(string fileName, string[] heads)
        : base(fileName, heads)
       {
        if (File.Exists(fileName))
        {
         File.Delete(fileName);
        }
       }
      }
     }


     

  • 相关阅读:
    使用Fiddle修改请求数据
    Fiddle抓包应用概述
    s = -1 #作用域之外的数字是不会改的
    python list.reverse() 方法 不可以 ss = li.reverse() ,这个列表翻转方法没有返回值, ss=None
    python 两个tuple元组之间连接判断是否有一个为空, and 和 & ,只能用and 不能用&
    http 协议最大url是不限制的,但实际上不会很长,有服务器的限制
    这个居然也可以python >>>geturl()["a"]
    python的字典可以这样子 print(dic["ab"+cd]) 震惊!!!
    mysql 远程 死活连不上 阿里云搞得个什么鬼
    txt默认的是个什么格式,anex,什么的,另存为utf-8或者unicode中文就不乱了
  • 原文地址:https://www.cnblogs.com/EddyPeng/p/2670589.html
Copyright © 2011-2022 走看看