C# - CSV file reader
来源:互联网 发布:盛世下的蝼蚁 知乎 编辑:程序博客网 时间:2024/06/06 07:42
// --------------------------------------------------------------------------------------------------------------------// <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); } } }}
42 0
- C# - CSV file reader
- C# read excel and CSV file
- Skip the header of a file with Python's CSV reader
- csv File.
- Python CSV Reader/Writer 例子
- python csv文件读取reader
- java file reader
- IO file reader /writer
- How to read data from csv file in c#
- Python CSV Reader/Writer/DictReader/DictWriter例子
- mysql C# reader
- Create a CSV file
- mysq load csv file
- ruby csv file
- read csv file. TextFieldParser
- mongoDB import csv file
- python 6-1 如何读写csv数据reader=csv.reader(rf)/writer=csv.writer(wf)
- export data to csv file
- UVA 11077 - Find the Permutations(递推)
- CSDN开源夏令营 百度数据可视化实践 ECharts(2)
- ruby on rails出现的问题ActiveModel::ForbiddenAttributesError
- 字符串
- 【Unity3D】【NGUI】如何动态给EventDelegate添加参数
- C# - CSV file reader
- OCP exam notes
- 数据挖掘笔记-分类-支持向量机SVM-1
- webApp前端开发技巧总结
- Azure编程笔记(5):长时间的异步操作带来的问题
- jQuery操作input改变value属性值
- ROR incompatible character encodings: UTF-8 and ASCII-8BIT 错误的 解决办法
- Find 7轻装版和正式版
- urlEncode&&urldecode的用法