用Javascript操作硬盘文件

有一个日志文件格式是这样的4129,23|4074,25|4011,16|……每组以|分隔,前者是更新的模板ID,后者是该模板被关联的频道,需要统计模板的关联频道数并排序。

输入:
4129,23|4074,25|4011,16|……
输出:
模板ID 关联频道数
3261 871
523 824
2039 811
……

html代码

  1. <form>
  2. <input type="file" size="30" id="filePath" />
  3. <input type="button" value="分析" id="analyze" />
  4. </form>

javascript处理代码

  1. function $(el){ return document.getElementById(el);}
  2. var analyze = function(){
  3.     this.analyze = 'analyze';
  4.     this.filePath = 'filePath';
  5.     this.readFlag = 1;
  6.     this.writeFlag = 2;
  7.     //匹配数据组的正则表达式
  8.     this.regPattern = /^(\d+),(\d+)$/;
  9.     this.cache = {result:{},array:[]};
  10.     this.fso = new ActiveXObject("Scripting.FileSystemObject");
  11. };
  12. analyze.prototype = {
  13.     start:function(){
  14.         var _filePath = $(this.filePath).value;
  15.         var pairArray = this.read(_filePath).split('|'),pair = null;
  16.         for(var i = 0 ; i < pairArray.length ; i++){
  17.             //如果正则表达式匹配成功
  18.             //则pair[0]为全匹配,pair[1]为第一个子匹配,pair[2]为第二个子匹配
  19.             pairpairArray[i].match(this.regPattern);
  20.             if(pair){
  21.                 if(this.cache.result[pair[1]] != undefined && this.cache.result[pair[1]] != null)
  22.                     this.cache.result[pair[1]]++;
  23.                 else
  24.                     this.cache.result[pair[1]] = 1;
  25.             }
  26.         }
  27.         //从result拷贝到array,方便快速排序
  28.         for(var i in this.cache.result){
  29.             this.cache.array.push({templateId:i,channelCount:this.cache.result[i]});
  30.         }   
  31.         this.cache.array = this.quickSort(this.cache.array);
  32.         this.write(this.quickSort(this.cache.array),_filePath.replace('.txt','')+'_statistics.txt');
  33.         alert('done!');
  34.     },
  35.     read:function(filePath){
  36.         var _file = this.fso.OpenTextFile(filePath, this.readFlag);
  37.         var _string = _file.ReadAll();
  38.         _file.close();
  39.         return _string;
  40.     },
  41.     write:function(array,filePath){
  42.         var _file = this.fso.OpenTextFile(filePath, this.writeFlag,true);
  43.         _file.WriteLine('模板ID    关联频道数');
  44.         for(var i in array){
  45.             _file.WriteLine(this.digitFix(array[i]['templateId'],4)+'     '+ this.digitFix(array[i]['channelCount'],4));
  46.         }
  47.         _file.close();
  48.     },
  49.     quickSort:function(array,left,right){
  50.         if(left == null && right == null){ left = 0; right = array.length-1;}
  51.         var swap = function(i,j){
  52.             if(i==j) return;
  53.             var temp = _array[i];
  54.             _array[i] = _array[j];
  55.             _array[j] = temp;
  56.         }
  57.         var i, last,_array = array;
  58.         if (left >= right) return _array;
  59.         last = left;
  60.         for (i = left + 1; i <= right;i++ ){
  61.             if(this.compare(_array[i], _array[left]))
  62.                 swap(++last,i);
  63.         }
  64.         swap(left,last);
  65.         this.quickSort(_array,left,last-1);
  66.         this.quickSort(_array,last+1,right);
  67.         return _array;
  68.     },
  69.     compare:function(x,y){
  70.         return x['channelCount'] > y['channelCount'];
  71.     },
  72.     digitFix:function(number,count){
  73.         var _string = number + '';
  74.         var _count = count-_string.length;
  75.         for(var i = 0; i < _count; i++)
  76.             _string = ' ' + _string;
  77.         return _string;
  78.     },
  79.     bind:function(el,type,fn,range,params){
  80.         var _params = params == null || params.constructor != Array  ? [params] : params;
  81.         if (el.addEventListener)
  82.             el.addEventListener(type,function(){fn.apply(range,_params)}, false);
  83.         else
  84.             el.attachEvent("on"+type, function(){fn.apply(range,_params)});
  85.     },
  86.     init:function(){
  87.         this.bind($(this.analyze),'click',this.start,this);
  88.     }
  89. };
  90. var aa = new analyze();
  91. aa.init();
[ 分类: 学习 Learning ] 由 Pan 发表于 January 27, 2008 6:20 pm  固定链接 

用Javascript操作硬盘文件》 这篇文章一共有1 条评论   我也想说两句

  1. 潘魏增|学海无涯 » 文章归档 » c#版快速排序

    January 27, 2008 6:22 pm

    […] 最新发表文章 用Javascript操作硬盘文件简单的Javascript日历减肥参考数据使用豆瓣API显示图书列表Javascript区块编码的一个示例几款网页分析软件介绍大斌训练频道Javascript实现命名空间率土之滨 莫非王臣历史归档 […]

RSS feed for comments on this post. TrackBack URI

相关文章:

发表评论