C# - CSV file reader

// --------------------------------------------------------------------------------------------------------------------
// <summary>
//   Defines the CSVFileReader type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace CSVFileReader
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Text;

    /// <summary>
    /// Reads a CSV file.
    /// </summary>
    public class CSVFileReader
    {
        /// <summary>
        /// The stream reader to process the CSV file reading.
        /// </summary>
        private StreamReader streamReader;

        /// <summary>
        /// Initializes a new instance of the <see cref="CSVFileReader"/> class.
        /// </summary>
        /// <param name="csvFilePath">
        /// The CSV file path.
        /// </param>
        /// <param name="delimiter">
        /// The delimiter.
        /// </param>
        public CSVFileReader(string csvFilePath, char delimiter)
        {
            this.CSVFilePath = csvFilePath;
            this.Delimiter = delimiter;
        }

        /// <summary>
        /// Finalizes an instance of the <see cref="CSVFileReader"/> class. 
        /// </summary>
        ~CSVFileReader()
        {
            this.Close();
        }

        /// <summary>
        /// Gets the CSV file path being read.
        /// </summary>
        public string CSVFilePath { get; private set; }

        /// <summary>
        /// Gets the delimiter used within the CSV file.
        /// </summary>
        public char Delimiter { get; private set; }

        /// <summary>
        /// Read a line from the CSV file into a list of strings.
        /// </summary>
        /// <returns>
        /// The list of string.
        /// </returns>
        public List<string> ReadLine()
        {
            this.Open();
            var resultElements = new List<string>();
            try
            {
                var currentLine = this.streamReader.ReadLine();
                if (currentLine != null)
                {
                    var currentLineElements = currentLine.Split(this.Delimiter);
                    resultElements.AddRange(currentLineElements);
                }
            }
            catch (Exception)
            {
                this.Close();
            }

            return resultElements;
        }

        /// <summary>
        /// Opens the stream reader.
        /// </summary>
        private void Open()
        {
            if (this.streamReader == null)
            {
                this.streamReader = new StreamReader(this.CSVFilePath, Encoding.GetEncoding(1252));
            }
        }

        /// <summary>
        /// Close the stream reader.
        /// </summary>
        private void Close()
        {
            if (this.streamReader == null)
            {
                return;
            }

            this.streamReader.Close();
            this.streamReader.Dispose();
        }
    }
}
// --------------------------------------------------------------------------------------------------------------------
// <summary>
//   Defines the Program type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace CSVFileReader
{
    using System;
    using System.Collections.Generic;

    /// <summary>
    /// The program.
    /// </summary>
    public static class Program
    {
        /// <summary>
        /// The main method.
        /// </summary>
        public static void Main()
        {
            var foo = new CSVFileReader(@"C:\Users\Administrator\Desktop\Tmp.csv", ',');
            List<string> line;
            while ((line = foo.ReadLine()).Count != 0)
            {
                foreach (var item in line)
                {
                    Console.Write(item + "|");
                }

                Console.WriteLine(string.Empty);
            }
        }
    }
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

转载于:https://www.cnblogs.com/lcchuguo/p/4677323.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值