110,473
社区成员
发帖
与我相关
我的任务
分享
public static List<String[]> ReadCSV(string filePathName)
{
List<String[]> ls = new List<String[]>();
StreamReader fileReader=new StreamReader(filePathName);
string strLine="";
while (strLine != null)
{
strLine = fileReader.ReadLine();
if (strLine != null && strLine.Length>0)
{
ls.Add(strLine.Split(','));
//Debug.WriteLine(strLine);
}
}
fileReader.Close();
return ls;
}
public static void WriteCSV(string filePathName,bool append, List<String[]> ls)
{
StreamWriter fileWriter=new StreamWriter(filePathName,append,Encoding.Default);
foreach(String[] strArr in ls)
{
fileWriter.WriteLine(String.Join (“;",strArr) );
}
fileWriter.Flush();
fileWriter.Close();
}
static void Main(string[] args)
{
string path = "data.csv";
try
{
StreamReader sr = new StreamReader(path);
string line;
while ((line = sr.ReadLine()) != null)
{
string[] arrStr = line.Split(';');
if (arrStr.Length != 5)
{
throw new ApplicationException("列数有误:" + line);
}
string aaaa = arrStr[0];
if(!Regex.IsMatch(aaaa,"^\\d{6}$"))
{
throw new ApplicationException("id有误:" + arrStr[0]);
}
if (!Regex.IsMatch(arrStr[2], "^[A-Z]$"))
{
throw new ApplicationException("class有误:" + arrStr[2]);
}
if (!Regex.IsMatch(arrStr[3], @"^(\w)+(\.\w+)*@(\w)+((\.\w+)+)$"))
{
throw new ApplicationException("email有误:" + arrStr[3]);
}
// ......
Console.WriteLine(line);
}
sr.Dispose();
sr.Close(); // 关闭流
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadKey();
}