用JS读取.txt文件方法以及一个操作.txt文件通用类方法【深海原创】

一.js读取文件示例:
var Arr=new Array();
try
{
 var fso=new ActiveXObject("scripting.filesystemobject_0418s");
 var txtstream=fso.openTextFile('c://data.txt');
 var txt='';
 while(!txtstream.atEndOfLine)
 {
   Arr.push(txtstream.readLine());
 }
 txtstream.close();
 txtstream=null;
 fso=null;
}
catch(e){alert(e);}
for(var i=0;i<Arr.length;i++)
  alert(Arr[i]);
 

data.txt
1
2
3
4
5
6
二.通用的类文件:
//*************************************通用函数类************************************
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.IO;

namespace faxapp.common
{
    public class commonfunction
    {
        //***********************************文件写入函数**************************************
        public static int buildDatafile(string fp,string[] content)
        {
            try
            {
                if (!File.Exists(fp))
                {
                    /* StreamWriter SW = File.CreateText(fp);
                     for(int i=0;i<content.Length;i++)
                     {
                         SW.WriteLine(content[i]);
                     }
                     SW.Flush();
                     SW.Close()*/
                    File.WriteAllLines(fp, content);
                }
                return 0;
            }
            catch (Exception)
            {
                return 1;
           
            }
           
        }
        //******************************文件删除函数**********************************
        public static int delDatafile(string fp)
        {
            try
            {
                File.Delete(fp);
                return 0;
            }
            catch (Exception)
            {
                return 1;
            }
        }
       //*******************************文字截断加……函数*******************************
        public static string cutStr(string str, int num)
        {
            if ((str.Length) > num)
            {
                str = "&nbsp;&nbsp;" + str.Substring(0, num) + "...";
                return str;
            }
            else
            {
                return str;
            }
        }
 

       //********************************字符串断开截取函数*********************************
       //Mondify By LiFuyun
       //******* para :参数; 0则返回未处理的字符串数组;1返回传真号码数组;2返回人名数组;3返回传真格式数组*******
        public static string[] getstringlist(string content, int para)
        {
            string[] stringtotal;
            string[] stringno;
            string[] stringname;
            string[] stringfax;
          stringtotal=content.Split(';');
          stringno = content.Split(';');
          stringname = content.Split(';');
          stringfax = content.Split(';');
          int namelen;
        
          for (int i = 0; i < stringtotal.Length; i++)
          {
              if ((stringtotal[i].LastIndexOf(">") == -1) || stringtotal[i].IndexOf("<") == -1 || stringtotal[i].IndexOf("<")!=0)
              {
                  stringname[i] = stringtotal[i];
                  stringno[i] = stringtotal[i];
                  stringfax[i] = stringno[i];
             
              }else
              {
                  namelen=stringtotal[i].LastIndexOf(">")-1;
                  stringname[i]=stringtotal[i].Substring(1, namelen);
                  stringno[i] = stringtotal[i].Substring(namelen + 2);
                  stringfax[i] = stringno[i] + " " + stringname[i];
              }
          }
          if (para == 0)
          {
              return stringtotal;
          }
          else if (para == 1)
          {
              return stringno;
          }
          else if (para == 2)
          {
              return stringname;
          }
          else
          {
              return stringfax;
          }
       
       
        }
    //**********************************asp。net弹出窗口函数,需要定义label控件*************************** 
        public static string AlertMsg(string str)
        {
            return "<script>alert('" + str + "');</script>";
       
      
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值