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);
}
}
}
}
版权声明:本文博客原创文章,博客,未经同意,不得转载。
C# - CSV file reader的更多相关文章
- Qt Read and Write Csv File
This page discusses various available options for working with csv documents in your Qt application. ...
- Drag & Drop and File Reader
参考 : http://www.html5rocks.com/zh/tutorials/file/dndfiles/ http://blog.csdn.net/rnzuozuo/article/det ...
- ogr2ogr: Export Well Known Text (WKT) for one feature to a CSV file
Perhaps you’re looking for this? ogr2ogr -f “CSV” “E:\4_GIS\NorthArkCartoData\UnitedStates\MO_wkt” “ ...
- SQL SERVER – Import CSV File Into SQL Server Using Bulk Insert – Load Comma Delimited File Into SQL Server
CSV stands for Comma Separated Values, sometimes also called Comma Delimited Values. Create TestTabl ...
- [PowerShell Utils] Create a list of virtual machines based on configuration read from a CSV file in Hyper-V
Hello everyone, this is the third post of the series. . Background =============== In my solution, ...
- python之模块csv之 读取CSV文件(reader和DictReader2个方法)
# -*- coding: utf-8 -*- #python 27 #xiaodeng #读取CSV文件(reader和DictReader2个方法) import csv #csv文件,是一种常用 ...
- Extending JMeter – Creating Custom Config Element – Property File Reader
JMeter is one of the best open source tools in the Test Automation Community. It comes with all the ...
- Python: Write UTF-8 characters to csv file
To use codecs, we can write UTF-8 characters into csv file import codecs with open('ExcelUtf8.csv', ...
- save tracking results into csv file for oxuva long-term tracking dataset (from txt to csv)
save tracking results into csv file for oxuva long-term tracking dataset (from txt to csv) 2019-10-2 ...
随机推荐
- 开源项目之Android 结束篇
随着公司新的需求以及Android嵌入式的深入,我已经没有多余的时间去扩展学习与Sip或UI不相关的Android开源项目,至此结束! 感想:研究Android已经一年半载了,白天忙公司项目,晚上扩展 ...
- 《深入理解mybatis原理》 MyBatis事务管理机制
MyBatis作为Java语言的数据库框架,对数据库的事务管理是其很重要的一个方面.本文将讲述MyBatis的事务管理的实现机制. 首先介绍MyBatis的事务Transaction的接口设计以及其不 ...
- innerHTML与appendChild(newnodeText)的区别
innerHTML和createTextNode都可以把一段内容添加到一个节点中,区别是如果这段内容中有html标签时表现就不同了,在createTextNode中会当作文本处理,不会被浏览器解析,但 ...
- Linux目录结构和常用命令
源地址:http://www.cnblogs.com/JCSU/articles/2770249.html 一.Linux目录结构 你想知道为什么某些程序位于/bin下,或者/sbin,或者/usr/ ...
- PHP+lghttpd + postgresql 环境搭建
PHP+lghttpd + postgresql 环境搭建 Linux 下PHP环境搭建 安装环境:PHP+lghttpd + postgresql 1:yum install lighttpd 红旗 ...
- Wi-Fi万能钥匙:说是破解,其实有危险(转)
Wi-Fi 万能钥匙如此危险,怎样做才能让这种可能严重侵害公众利益的 app 在中国消失? 这个“钥匙”为什么能够破解 Wi-Fi?它真的是“破解” Wi-Fi 吗?两年前我就有这个疑问了,原谅我对一 ...
- 属性“dataProvider”有多个初始值设定项。(注意:“dataProvider”是“mx.charts.BarChart”的默认属性)。
1.错误描写叙述 属性"dataProvider"有多个初始值设定项.(注意:"dataProvider"是"mx.charts.BarChart&q ...
- POJ 2365 Rope(水题)
[题意简述]:给出我们钉子个数与半径,让我们求出缠绕在钉子上的绳子有多长. [分析]:从题目中我们能够看出,绳子长度的和等于每两个钉子的距离的和加上接触在钉子上的绳子的长度,不难发现这部分长度事实上就 ...
- Servlet(二)GenericServlet
以GenericServlet创建Servlet 1.在FirstServlet同文件夹下建立GServlet.java package com.hunhun; import javax.servle ...
- 百度富文本编辑器UEditor1.3上传图片附件等
今天一直在整我的一个项目的编辑器上传图片,我用的是百度UEditor 1.3版本号的:如今已经有了1.4的了,只是还算比較新吧,可是官网上面没有上传图片这些的教程,而网上对于这方面的资料非常少啊,折腾 ...