C # Read the file content and store it in the int array array.txt

Source: Internet
Author: User

Using system;
Using system. Data;
Using system. configuration;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Using system. collections;
Using system. IO;
Using system. text;

/// <Summary>
/// Summary description for readfile
/// </Summary>
Public class readfile
{
Public readfile ()
{
//
// Todo: Add constructor logic here
//
}

Public int [,] readfiletoarray ()
{

Int [,] iret = NULL;

Arraylist alnumline = getfilecontent ();
String [] strlinearr = NULL;
If (alnumline. Count> 0)
{
Strlinearr = convert. tostring (alnumline [0]). Trim (','). Split (',');
Iret = new int [alnumline. Count, strlinearr. Length];
For (INT I = 0; I <alnumline. Count; I ++)
{
Strlinearr = convert. tostring (alnumline [I]). Trim (','). Split (',');
For (Int J = 0; j <strlinearr. length; j ++)
{
Iret [I, j] = convert. toint32 (strlinearr [J]);
}
}
}

Return iret;

}

Public arraylist getfilecontent ()
{

Arraylist alret = new arraylist ();

String strfilepath = httpcontext. Current. server. mappath ("~ ") +"/Array.txt ";

If (! File. exists (strfilepath ))
{
Httpcontext. Current. response. Write ("file [" + strfilepath + "] does not exist. ");
Return alret;
}

Try
{
// Read a line of text and temporarily store it in the arraylist
Streamreader sr = new streamreader (strfilepath, encoding. getencoding ("gb2312 "));
String L;
While (L = Sr. Readline ())! = NULL)
{
If (! String. isnullorempty (L. Trim ()))
Alret. Add (L. Trim ());
}
Sr. Close ();
}
Catch (ioexception ex)
{
Httpcontext. Current. response. Write ("An error occurred while reading the file! Check whether the file is correct. ");
Httpcontext. Current. response. Write (ex. tostring ());
}

Return alret;

}
}

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.