文本文件处理封装类源代码
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.IO;
namespace MyQuery.Utils
{
/// <summary>
/// 封装Txt文件处理
/// by 贾世义 2011-5-27
/// </summary>
public static class TxtHelper
{
/// <summary>
/// 加载txt数据到DataSet
/// </summary>
/// <param name="fileName">完整路径txt文件名 以.txt为后缀</param>
/// <param name="encoding">字符类型 默认GBK</param>
/// <param name="split">分割字符</param>
/// <param name="isFirstTitle">首行是否为标题行</param>
/// <returns></returns>
public static DataSet GetDataFromTxt(string fileName, string encoding, char split, bool isFirstTitle)
{
DataSet ds = null;
if (IsTxtFile(fileName) && File.Exists(fileName))
{
DataTable dt = new DataTable();
Encoding ecode;
if (String.IsNullOrEmpty(encoding))
{
ecode = Encoding.GetEncoding("GBK");
}
else
{
ecode = Encoding.GetEncoding(encoding);
}
int i = 0;
string[] lines = File.ReadAllLines(fileName, ecode);
foreach (string line in lines)
{
string[] options = DataHelper.GetStrings(line, split);
if (options != null)
{
//列
if (i == 0)
{
for (int j = 0; j < options.Length; j++)
{
string n = null;
if (isFirstTitle)
{
n = options[j];
}
if (String.IsNullOrEmpty(n))
{
n = j.ToString();
}
dt.Columns.Add(n);
}
i = 1;
if (isFirstTitle)
{
continue;
}
&
补充:软件开发 , C# ,