c# 讀取文件內(nèi)容存放到int數(shù)組 array.txt
更新時(shí)間:2009年04月17日 21:59:15 作者:
c# 讀取文本的內(nèi)容,并且將內(nèi)容保存到int數(shù)組中,大家可以學(xué)習(xí)到c#一些數(shù)組跟讀取內(nèi)容的函數(shù)。
復(fù)制代碼 代碼如下:
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("文件[" + strFilePath + "]不存在。");
return alRet;
}
try
{
//讀出一行文本,并臨時(shí)存放在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("讀文件出錯(cuò)!請(qǐng)檢查文件是否正確。");
HttpContext.Current.Response.Write(ex.ToString());
}
return alRet;
}
}
相關(guān)文章
.NET Core讀取配置文件方式詳細(xì)總結(jié)
這篇文章主要為大家詳細(xì)總結(jié)了.NET Core讀取配置文件方式,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08
.NET Core中HttpClient的正確打開(kāi)方式
這篇文章主要給大家介紹了關(guān)于.NET Core中HttpClient的正確打開(kāi)方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-01-01
Linkbutton控件在項(xiàng)目中的簡(jiǎn)單應(yīng)用
Button控件可分為button控件、LinkButton控件、ImageButton控件三類,而LinkButton控件則在頁(yè)面上顯示為一個(gè)超級(jí)鏈接,下面與大家分享下其具體應(yīng)用2013-05-05
ASP.NET MVC小結(jié)之基礎(chǔ)篇(二)
本文續(xù)上篇文章,還是介紹些asp.net mvc相關(guān)的基礎(chǔ)知識(shí),非常的詳細(xì),新手朋友們看看,高手們略過(guò)吧2014-11-11
關(guān)于Metalama使用Fabric操作項(xiàng)目或命名空間的問(wèn)題
Metalama是一個(gè)基于微軟編譯器Roslyn的元編程的庫(kù),可以解決我在開(kāi)發(fā)中遇到的重復(fù)代碼的問(wèn)題,這篇文章主要介紹了Metalama使用Fabric操作項(xiàng)目或命名空間,需要的朋友可以參考下2022-04-04
在ASP.NET?MVC下限制同一個(gè)IP地址單位時(shí)間間隔內(nèi)的請(qǐng)求次數(shù)的解決方法
有時(shí)候,當(dāng)用戶請(qǐng)求一個(gè)Controller下的Action,我們希望,在單位時(shí)間間隔內(nèi),比如每秒,每分鐘,每小時(shí),每天,每星期,限制同一個(gè)IP地址對(duì)某個(gè)Action的請(qǐng)求次數(shù),如何做呢?這篇文章主要介紹了在ASP.NET?MVC下限制同一個(gè)IP地址單位時(shí)間間隔內(nèi)的請(qǐng)求次數(shù),需要的朋友可以參考下2024-01-01

