CSV文件,是指使用逗号对数据进行分割的文本数据文件。昨天有人提出了一个问题,就是怎么对CSV文件进行操作,并且给出了一个类的定义。我根据这个类定义实现了一个能够读些CSV文件的类。
由于涉及到了字符串操作,为了提高查询、替换性能,我使用了正则表达式。CSV文件打开后,会被读到本地的一个缓冲区中进行操作,然后通过调用Submit()函数项文件中更新。
代码:
using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
namespace CSVFileClass
{
/// <summary>
/// Summary description for CCSVFile.
/// </summary>
public class CCSVFile : IDisposable
{
#region Fields
private System.IO.FileStream fileStream;
private System.IO.StreamReader streamReader;
private System.IO.StreamWriter streamWriter;
private string FileName;
private System.Collections.ArrayList strList = new System.Collections.ArrayList();
private System.Text.RegularExpressions.Regex MatchField = new Regex(".*?,");
private System.Text.RegularExpressions.Regex comma = new Regex(",");
#endregion
#region Functions
public CCSVFile(string strFileName)
{
//
// TODO: Add constructor logic here
//
this.FileName = strFileName;
OpenFile();
}
//写一行数据,iLineIndex表示写哪一行,strLineContent表示写这一行的内容。
public void WriteLine(int iLineIndex, string strLineContent)
{
if(iLineIndex<=0||iLineIndex>GetLines()+1)
throw new ArgumentOutOfRangeException("iLineIndex","iLineIndex is out of range");
this.strList[iLineIndex-1] = strLineContent;
}
//读一行数据,iLineIndex表示读哪一行的数据。
public string ReadLine(int iLineIndex)
{
if(iLineIndex<=0||iLineIndex>GetLines())
{
throw new ArgumentOutOfRangeException("iLineIndex","iLineIndex is out of range");
}
string str;
str = (string)this.strList[iLineIndex-1];
return str;
}
//写一个字段的数据,iLineIndex表示哪一行,iFieldIndex表示哪个字段,strFieldContent表示字段内容,检查strFieldContent是否有逗号,如果内容中有就返回-1。
public bool WriteField(int iLineIndex, int iFieldIndex, string strFieldContent)
{
if (MatchField.IsMatch(strFieldContent))
return false;
if(iLineIndex<=0||iLineIndex>GetLines())
{
throw new ArgumentOutOfRangeException("iLineIndex","iLineIndex is out of range");
}
string str;
str = (string)this.strList[iLineIndex-1];
MatchCollection matchList = MatchField.Matches(str);
if(iFieldIndex<=0||iFieldIndex>matchList.Count)
return false;
strFieldContent +=",";
int i = matchList[iLineIndex-1].Index;
this.strList[iLineIndex-1] = MatchField.Replace(str,strFieldContent,1,matchList[iFieldIndex-1].Index);
return true;
}
//读字段的值。
public string ReadField(int iLineIndex, int iFieldIndex)
{
string result = null;
if(iLineIndex<=0||iLineIndex>GetLines())
{
throw new ArgumentOutOfRangeException("iLineIndex","iLineIndex is out of range");
}
string str;
str = (string)this.strList[iLineIndex-1];
MatchCollection matchList = MatchField.Matches(str);
if(iFieldIndex<=0||iFieldIndex>matchList.Count)
return comma.Replace(result,"");
result = matchList[iFieldIndex-1].Value.ToString();
return comma.Replace(result,"");
}
//得到需要读写的哪一行
private int GetLines()
{
return this.strList.Count;
}
//向文件提交数据
public void Submit()
{
this.streamReader.Close();
this.streamWriter = new StreamWriter(this.FileName);
foreach (string str in this.strList)
{
this.streamWriter.WriteLine(str);
}
this.streamWriter.Close();
OpenFile();
}
//打开指定文件
private void OpenFile()
{
try
{
fileStream = new FileStream(this.FileName,System.IO.FileMode.Open,System.IO.FileAccess.ReadWrite);
}
catch(System.IO.FileNotFoundException e)
{
this.fileStream = null;
throw new System.IO.FileNotFoundException(e.Message,e.FileName,e.InnerException);
}
string tmpString;
streamReader = new StreamReader(this.fileStream);
do
{
tmpString = this.streamReader.ReadLine();
if(null != tmpString)
strList.Add(tmpString);
}
while(null != tmpString);
}
public void Close()
{
this.Dispose();
}
#endregion
#region IDisposable 成员
public void Dispose()
{
// TODO: 添加 CCSVFile.Dispose 实现
this.fileStream.Close();
this.streamReader.Close();
this.streamWriter.Close();
}
#endregion
}
}
转载于:https://www.cnblogs.com/net66/archive/2005/07/26/200704.html