萬盛學電腦網

 萬盛學電腦網 >> 腳本專題 >> javascript >> jscript讀寫二進制文件的方法

jscript讀寫二進制文件的方法

   jscript讀寫二進制文件的方法

         這篇文章主要介紹了jscript讀寫二進制文件的方法,涉及javascript中ActiveXObject對象的使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下

  本文實例講述了jscript讀寫二進制文件的方法。分享給大家供大家參考。具體實現方法如下:

  var bin = new Array(256);

  for(var i=0;i<256;i++){

  bin[i]=String.fromCharCode(i);

  }

  function TestWrite(){

  var Stream = new ActiveXObject("ADODB.Stream");

  var adTypeBinary=1,adTypeText=2;

  Stream.Type = adTypeText;

  Stream.CharSet = "iso-8859-1";

  Stream.Open();

  //Stream.WriteText("x00x01x02xffxff");

  for(var i=0;i<256;i++){

  Stream.WriteText(String.fromCharCode(i));

  //Stream.WriteText(bin[i]);

  }

  Stream.SaveToFile("c:windowstemptest.bin", 2);

  Stream.Close();

  Stream = null;

  }

  function BinaryFile(filepath){

  var adTypeBinary=1,adTypeText=2;

  var adSaveCreateNotExist=1,adSaveCreateOverWrite=2;

  var adReadAll=-1,adReadLine=-2;

  this.path=filepath;

  this.WriteAll = function(content){

  var Stream = new ActiveXObject("ADODB.Stream");

  Stream.Type = adTypeText;

  Stream.CharSet = "iso-8859-1";

  Stream.Open();

  Stream.WriteText(content);

  Stream.SaveToFile(this.path, adSaveCreateOverWrite);

  Stream.Close();

  Stream = null;

  }

  this.ReadAll = function(){

  var Stream = new ActiveXObject("ADODB.Stream");

  Stream.Type = adTypeText;

  Stream.CharSet = "iso-8859-1";

  Stream.Open();

  Stream.LoadFromFile(this.path);

  var content = Stream.ReadText(adReadAll);

  Stream.Close();

  Stream = null;

  return content;

  }

  }

  用法示例如下:

  var crFolder = 'C:/Temp/cr'

  var bf1=new BinaryFile(crFolder+"/PCDV0026.JPG");

  var bf2=new BinaryFile(crFolder+"/PCDV0026_.JPG");

  bf2.WriteAll(bf1.ReadAll());

  希望本文所述對大家的javascript程序設計有所幫助。

copyright © 萬盛學電腦網 all rights reserved