本文实例讲述了jscript读写二进制文件的方法。分享给大家供大家参考。具体实现方法如下:
rush:js;">
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("\x00\x01\x02\xff\xff");
for(var i=0;i<256;i++){
Stream.WriteText(String.fromCharCode(i));
//Stream.WriteText(bin[i]);
}
Stream.SavetoFile("c:\\windows\\temp\\test.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;
}
}
用法示例如下:
rush:js;">
var crFolder = 'C:/Temp/cr'
var bf1=new BinaryFile(crFolder+"/PCDV0026.JPG");
var bf2=new BinaryFile(crFolder+"/PCDV0026_.JPG");
bf2.Writeall(bf1.ReadAll());
希望本文所述对大家的javascript程序设计有所帮助。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。