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 ...
随机推荐
- ExtJs4 笔记(3) Ext.Ajax 对ajax的支持
本篇主要介绍一下ExtJs常用的几个对JS语法的扩展支持,包括Ajax封装,函数事件操作封装,还有扩展的常用函数等.Ajax服务端交互式操作是提交到.NET MVC.后续服务端交互都采用这一方式实现. ...
- [Java学习笔记]Java Tips
1.Java没有sizeof关键字 , volatile是java关键字.详情见:http://www.cnblogs.com/aigongsi/archive/2012/04/01/2429166. ...
- [Cacti] memcache安装执行、cacti监控memcache实战
简单介绍 Memcache是danga.com的一个项目,最早是为 LiveJournal 服务的.眼下全世界不少人使用这个缓存项目来构建自己大负载的站点,来分担数据库的压力. Memcache官方站 ...
- RealThinClient学习(一)
服务端代码: unit RtcHttpServer; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, ...
- Mybatis 3 返回布尔值,需要注意的地方
在Mybatis中,有时候需要返回布尔值 ,来确定某个记录行是否存在. 例如: <select id="isExistCode" parameterType="st ...
- java thread reuse(good)
I have always read that creating threads is expensive. I also know that you cannot rerun a thread. I ...
- jvm调优经验分享
当Java程序申请内存,超出VM可分配内纯的时候,VM首先可能会GC,假设GC完还是不够,或者申请的直接超够VM可能有的,就会抛出内 存溢出异常.从VM规范中我们能够得到,一下几种异常. java.l ...
- 【Nginx】启动过程
从应用程序的启动过程中main功能开始跟踪. 解析命令行參数并保存到ngx_cycle_t结构体中,在ngx_process_options函数中将保存配置文件路径. 调用ngx_add_inheri ...
- Apache配置虚拟文件夹
作为一个Android开发人员,一直以为,至少应该有一个server语言,最近慢慢学习php,当然学习Apache使用.本文介绍Win7环境下,怎样配置Apache的虚拟文件夹. 首先,找到我们Apa ...
- 《深入理解OSGi:Equinox原理、应用与最佳实践》笔记_2_建立开发环境
本文对应书本5.1.3的内容 书本中通过CVS下载的源码 但是笔者实践的时候发现无法下载...地址已经失效了(也许是笔者的失误输错地址所致) 可以用git下载 地址是: http://git.ecli ...