C#中怎么读取文件内容并实现另存

发布时间:2021-07-07 16:22:43 作者:Leah
来源:亿速云 阅读:855

这篇文章给大家介绍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;   /// ///C#读取文件内容另存 /// Summary description for ReadFile  ///  public class ReadFile  {   public ReadFile()   {     //  C#读取文件内容   // TODO: Add constructor logic here   //      }        public int[,] ReadFileToArray()      {   int[,] iret = null;  //C#读取文件内容另存  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;       }   //C#读取文件内容另存     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;          }   //C#读取文件内容另存        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;   //C#读取文件内容另存     }  }

关于C#中怎么读取文件内容并实现另存就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

推荐阅读:
  1. 通编码读取文件内容
  2. ​ php中读取文件内容的方法

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

上一篇:C#中怎么实现一个文件下载

下一篇:C#中怎么对ini文件进行读写操作

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》