C#读取文件内容另存的实现

开发 后端
C#读取文件内容另存的实现是如何的呢?那么本文就向你介绍C#读取文件内容另存放到int数组 array.txt的实现。

C#读取文件内容另存的实现的如何呢?让我们看看代码的实现:

  1. using System;  
  2. using System.Data;  
  3. using System.Configuration;  
  4. using System.Web;  
  5. using System.Web.Security;  
  6. using System.Web.UI;  
  7. using System.Web.UI.WebControls;  
  8. using System.Web.UI.WebControls.WebParts;  
  9. using System.Web.UI.HtmlControls;  
  10. using System.Collections;  
  11. using System.IO;  
  12. using System.Text;  
  13.  
  14. /// 
  15. ///C#读取文件内容另存 
  16. /// Summary description for ReadFile  
  17. /// 
  18.  
  19. public class ReadFile  
  20. {  
  21.  public ReadFile()  
  22.  {  
  23.    //  C#读取文件内容
  24.    // TODO: Add constructor logic here  
  25.  //  
  26.     }  
  27.  
  28.  
  29.     public int[,] ReadFileToArray()  
  30.     {  
  31.  
  32. int[,] iret = null;  //C#读取文件内容另存
  33.  
  34.  ArrayList alNumLine = getFileContent();  
  35.    string[] strLineArr = null;  
  36.  if (alNumLine.Count > 0)  
  37.         {  
  38.     strLineArr = Convert.ToString(alNumLine[0]).Trim(',').Split(',');  
  39.   iret = new int[alNumLine.Count, strLineArr.Length];  
  40.     for (int i = 0; i < alNumLine.Count; i++)  
  41.             {  
  42.  strLineArr = Convert.ToString(alNumLine[i]).Trim(',').Split(',');  
  43.  for (int j = 0; j < strLineArr.Length; j++)  
  44.  {  
  45.     iret[i, j] = Convert.ToInt32(strLineArr[j]);  
  46.    }  
  47.             }  
  48.         }  
  49.  
  50.         return iret;  
  51.  
  52.     }  
  53.  //C#读取文件内容另存
  54.  
  55.     public ArrayList getFileContent()  
  56.     {  
  57.  
  58.  ArrayList alRet = new ArrayList();  
  59.  
  60.  string strFilePath = HttpContext.Current.Server.MapPath("~"
  61. "/array.txt";  
  62.  
  63.  if (!File.Exists(strFilePath))  
  64.         {  
  65.  HttpContext.Current.Response.Write("文件[" + strFilePath + "]不存在。");  
  66.             return alRet;  
  67.         }  
  68.  //C#读取文件内容另存
  69.         try 
  70.         {  
  71.  //读出一行文本,并临时存放在ArrayList中  
  72.     StreamReader sr = new StreamReader(strFilePath, 
  73. Encoding.GetEncoding("gb2312"));  
  74.   string l;  
  75.    while ((l = sr.ReadLine()) != null)  
  76.             {  
  77.    if (!string.IsNullOrEmpty(l.Trim()))  
  78.         alRet.Add(l.Trim());  
  79.             }  
  80.             sr.Close();  
  81.         }  
  82.         catch (IOException ex)  
  83.         {  
  84.   HttpContext.Current.Response.Write("读文件出错!请检查文件是否正确。");  
  85.   HttpContext.Current.Response.Write(ex.ToString());  
  86.         }  
  87.  
  88.         return alRet;  
  89.  //C#读取文件内容另存
  90.  
  91.     }  

C#读取文件内容另存的实现就向你介绍到这里,希望通过这样的介绍使你能够明白C#读取文件内容另存相关内容,希望对你有所帮助。

【编辑推荐】

  1. C#读取XML文档的实现浅析
  2. C#读取XML节点内容方法实例简析
  3. C#读取XML文档使用XMLTextReader类浅析
  4. C#读取文件夹中的文件操作浅析
  5. C#读取文件夹下面的全部文件的实现
责任编辑:仲衡 来源: 百度空间
相关推荐

2009-08-12 18:29:06

C#读取TXT文件

2009-09-01 11:21:02

C#读取word内容

2009-08-12 17:12:51

C#读取文件夹

2009-09-02 19:08:03

C#实现读取文本文件

2009-08-13 10:15:50

C#读取Excel

2009-08-21 16:13:27

C#读取资源文件

2009-08-13 09:32:00

C#读取TXT文件

2009-09-09 18:00:55

C# XML编程

2009-09-10 09:42:53

C# TextBox

2009-08-12 16:26:30

C#读取XML文档

2009-08-13 09:16:57

C#读取配置文件

2009-08-13 09:58:55

C#读取配置文件

2009-09-01 11:25:08

C#读取Word文件

2009-08-24 17:58:19

C#读取XML文件

2009-08-12 17:27:11

C#读取文件

2009-08-12 16:57:28

C#读取文件夹

2009-08-18 13:35:06

C#枚举文件

2009-08-12 16:38:35

C#读取XML节点

2023-10-31 12:59:00

C++编程语言

2009-08-21 17:48:13

C#读取文件信息
点赞
收藏

51CTO技术栈公众号