C# - CSV file reader

來源:互聯網
上載者:User

標籤:csv   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);            }        }    }}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.