c# 讀取文件內容存放到int數組 array.txt
更新時間:2009年04月17日 21:59:15 作者:
c# 讀取文本的內容,并且將內容保存到int數組中,大家可以學習到c#一些數組跟讀取內容的函數。
復制代碼 代碼如下:
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;
}
}
相關文章
ASP.NET MVC使用RazorEngine解析模板生成靜態(tài)頁
這篇文章主要介紹了ASP.NET MVC使用RazorEngine解析模板生成靜態(tài)頁的相關資料,需要的朋友可以參考下2016-05-05win7系統(tǒng)下 vs2010 調式就關閉要重啟處理方法
最近經常出現只要一使用vs2010進行調試,就提示關閉并要重啟,好煩,度娘了半天,總結下來解決方法,親測可用哦。2014-08-08使用vs2019加.net core 對WeiApi的創(chuàng)建過程詳解
這篇文章主要介紹了使用vs2019加.net core 對WeiApi的創(chuàng)建,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-07-07告別ADO.NET實現應用系統(tǒng)無縫切換的煩惱(總結篇)
說起ADO.NET,就扯上了數據庫訪問類庫了,現在的每個項目的數據庫訪問類應該說都很強的了,經常就聽到說我的我們的數據庫訪問類怎么怎么強大而且支持多數據庫,現在的大家做的項目里用的數據庫訪問類庫我想也都是支持多數據庫吧,支持到什么程度我就不知道了2009-11-11