当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > c# 读取文件内容存放到int数组 array.txt

ASP.NET
.net数据库连接池配置技巧(默认值)
.NET 数据库连接池
asp.net sqlconnection con.close和con.dispose区别
ASP.NET 多次提交的解决办法
ASP.NET 多次提交的解决办法2
firebird Embedded模式(.net 3.5)
js 父页中的单选按钮取值
js控制.net验证控件是否可用。
ASP.NET AJAX时用alert弹出对话框
aspx 中文汉字显示为乱码
C# 小数位数保留的方法集锦
C# Math.Round()函数问题
C# ToString格式大全
ASP.net Forms验证Demo
asp.net 文章内容分页显示的代码
asp.net两级联动(包含添加和修改)
TreeView创建IHierarchicalDataSource类型的数据源实现
ASP.NET输出PNG图片时出现GDI+一般性错误的解决方法
Ajax.net 显示错误信息的设置
asp.net ListView 数据绑定

ASP.NET 中的 c# 读取文件内容存放到int数组 array.txt


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-13   浏览: 87 ::
收藏到网摘: n/a

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;
}
}