C # encapsulate TXT file processing

Source: Internet
Author: User

Using system;
Using system. Collections. Generic;
Using system. text;
Using system. Data;
Using system. IO;

Namespace myquery. utils
{
/// <Summary>
/// Encapsulate TXT file processing
/// By Jia Shiyi 2011-5-27
/// </Summary>
Public static class txthelper
{
/// <Summary>
/// Load TXT data to Dataset
/// </Summary>
/// <Param name = "FILENAME"> the complete path TXT file name is suffixed with .txt. </param>
/// <Param name = "encoding"> default character type: GBK </param>
/// <Param name = "split"> delimiter </param>
/// <Param name = "isfirsttitle"> whether the first line is a title line </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)
{
// Column
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;
}
}
Else if (options. length> DT. Columns. Count)
{
For (Int J = 0; j <options. Length-Dt. Columns. Count; j ++)
{
DT. Columns. Add (Dt. Columns. Count. tostring ());
}
}
// Data
Datarow DR = DT. newrow ();
For (Int J = 0; j <options. length; j ++)
{
Dr [J] = options [J];
}
DT. Rows. Add (DR );
}
}
If (Dt. Rows. Count> 0)
{
DS = new dataset ();
DS. Tables. Add (DT );
}
}
Return Ds;
}

/// <Summary>
/// Whether the suffix type is supported by TXT reading
/// </Summary>
/// <Param name = "FILENAME"> file name </param>
/// <Returns> </returns>
Public static bool istxtfile (string filename)
{
Return! String. isnullorempty (filename )&&
(Filename. endswith (". txt", stringcomparison. currentcultureignorecase)
| Filename. endswith (". CSV", stringcomparison. currentcultureignorecase ));
}
/// <Summary>
/// Read text
/// </Summary>
/// <Param name = "FILENAME"> complete file name </param>
/// <Returns> </returns>
Public static string getstring (string filename)
{
String result = NULL;
If (file. exists (filename ))
{
Result = file. readalltext (filename, encoding. utf8 );
}
Return result;
}
/// <Summary>
/// Write text to a text file
/// </Summary>
/// <Param name = "name"> complete file name </param>
/// <Param name = "content"> content </param>
/// <Param name = "iscover"> whether to overwrite existing files </param>
Public static void writetofile (string name, string content, bool iscover)
{
Filestream FS = NULL;
Try
{
If (! Iscover & file. exists (name ))
{
FS = new filestream (name, filemode. append, fileaccess. Write );
Streamwriter Sw = new streamwriter (FS, encoding. utf8 );
Sw. writeline (content );
Sw. Flush ();
Sw. Close ();
}
Else
{
File. writealltext (name, content, encoding. utf8 );
}
}
Finally
{
If (FS! = NULL)
{
FS. Close ();
}
}

}
}
}

Welcome to: http: // 121.18.78.216, which is a demonstration platform for ease of query and analysis, workflow, content management, and project management.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.