c# 讀取文件內(nèi)容存放到int數(shù)組 array.txt
更新時間:2009年04月17日 21:59:15 作者:
c# 讀取文本的內(nèi)容,并且將內(nèi)容保存到int數(shù)組中,大家可以學習到c#一些數(shù)組跟讀取內(nèi)容的函數(shù)。
復制代碼 代碼如下:
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
{
//讀出一行文本,并臨時存放在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("讀文件出錯!請檢查文件是否正確。");
HttpContext.Current.Response.Write(ex.ToString());
}
return alRet;
}
}
相關(guān)文章
ASP.NET MVC小結(jié)之基礎(chǔ)篇(二)
本文續(xù)上篇文章,還是介紹些asp.net mvc相關(guān)的基礎(chǔ)知識,非常的詳細,新手朋友們看看,高手們略過吧2014-11-11
關(guān)于Metalama使用Fabric操作項目或命名空間的問題
Metalama是一個基于微軟編譯器Roslyn的元編程的庫,可以解決我在開發(fā)中遇到的重復代碼的問題,這篇文章主要介紹了Metalama使用Fabric操作項目或命名空間,需要的朋友可以參考下2022-04-04
在ASP.NET?MVC下限制同一個IP地址單位時間間隔內(nèi)的請求次數(shù)的解決方法
有時候,當用戶請求一個Controller下的Action,我們希望,在單位時間間隔內(nèi),比如每秒,每分鐘,每小時,每天,每星期,限制同一個IP地址對某個Action的請求次數(shù),如何做呢?這篇文章主要介紹了在ASP.NET?MVC下限制同一個IP地址單位時間間隔內(nèi)的請求次數(shù),需要的朋友可以參考下2024-01-01

