c# 读取文件内容存放到int数组 array.txt_实用技巧

复制代码 代码如下:

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

时间: 2024-07-31 08:44:26

c# 读取文件内容存放到int数组 array.txt_实用技巧的相关文章

ASP.NET实现读取Excel内容并在Web上显示_实用技巧

本文实例讲述了ASP.NET实现读取Excel内容并在Web上显示的方法,是非常实用的一个功能,分享给大家供大家参考.具体实现方法如下: 点击事件代码.cs代码如下: protected void Button1_Click(object sender, EventArgs e) { string strPath = "d:/test.xls"; string mystring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source

php读取文件内容到数组的方法

这篇文章主要介绍了php读取文件内容到数组的方法,涉及php中file.rtrim等函数对文件及字符串的操作技巧,具有一定参考借鉴价值,需要的朋友可以参考下     本文实例讲述了php读取文件内容到数组的方法.分享给大家供大家参考.具体分析如下: php中可以通过file()函数将文件读取到数组中,数组中的元素即为文件的每行,file()函数通过"n"按行分割文件保存到数组,所以数组每个元素都是以"n"结尾,我们可以通过 rtrim()函数将其去除 ? 1 2 3

php读取文件内容到数组的方法_php技巧

本文实例讲述了php读取文件内容到数组的方法.分享给大家供大家参考.具体分析如下: php中可以通过file()函数将文件读取到数组中,数组中的元素即为文件的每行,file()函数通过"\n"按行分割文件保存到数组,所以数组每个元素都是以"\n"结尾,我们可以通过 rtrim()函数将其去除 <?php $lines = file("/tmp/file.txt"); foreach ($lines as $line) { $line = r

java库 读取文件内容并转换为数组

问题描述 java库读取文件内容并转换为数组 解决方案 解决方案二:基本问题就慢慢一步步来,先读文件,再去想如何转为数组.解决方案三:自己实现或者调用com.google.common.io.Filesapache的工具类.

fflush函数-关于写一个https客户端在指定位置读取文件内容

问题描述 关于写一个https客户端在指定位置读取文件内容 在代码段最后的while循环中,用SSL_read函数读取文件远程的内容,但是内容读取后,总有小部分内容的缺失,while程序执行完后,并没有执行while之后的内容,程序运行到这里就不运行了 int main(int argc char *argv[]){ int sock_fd ret; struct sockaddr_in server_addr; char requestBuffer[2048]={0}; char respon

php读取文件内容的三种方法

 这篇文章主要介绍了php读取文件内容的三种方法,需要的朋友可以参考下 php读取文件内容的三种方法:    //**************第一种读取方式*****************************  代码如下: header("content-type:text/html;charset=utf-8");  //文件路径  $file_path="text.txt";  //判断是否有这个文件  if(file_exists($file_path)

java-Java中用OutputStream读取文件内容。

问题描述 Java中用OutputStream读取文件内容. import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.io.IOException; import java.io.FileNotFoundException; public class Cc { public static void main(String args[]){ /*private*/ in

php读取文件内容的三种可行方法示例介绍_php技巧

php读取文件内容的三种方法: //**************第一种读取方式***************************** 复制代码 代码如下: header("content-type:text/html;charset=utf-8"); //文件路径 $file_path="text.txt"; //判断是否有这个文件 if(file_exists($file_path)){ if($fp=fopen($file_path,"a+&quo

Android 读取文件内容实现方法总结_Android

Android 读取文件内容实现方法,这里整理了几种方法,大家需要可以看下. 如果要打开存放在/data/data/<package name>/files目录应用私有的文件,可以使用Activity提供openFileInput()方法. FileInputStream inStream = this.getContext().openFileInput("itcast.txt"); Log.i("FileTest", readInStream(inS