开发者

How to read a local file by chunks in JavaScript?

开发者 https://www.devze.com 2023-03-18 08:28 出处:网络
Say, I have a file that is 500 bytes in size on my local hard drive and I want to read first 100 bytes from it without loading the whole f开发者_如何学Cile into memory. How to accomplish that in JavaS

Say, I have a file that is 500 bytes in size on my local hard drive and I want to read first 100 bytes from it without loading the whole f开发者_如何学Cile into memory. How to accomplish that in JavaScript with the help of UniversalXPConnect? In Firefox only, of course.


Assuming that you want to read ASCII text data (no character set conversion):

var file = Components.classes["@mozilla.org/file/local;1"]
                     .createInstance(Components.interfaces.nsILocalFile);
file.initWithPath("/foo/bar");
var fstream = Components.classes["@mozilla.org/network/file-input-stream;1"]
                        .createInstance(Components.interfaces.nsIFileInputStream);
fstream.init(file, -1, 0, 0);
var sstream = Components.classes["@mozilla.org/scriptableinputstream;1"]
                        .createInstance(Components.interfaces.nsIScriptableInputStream);
sstream.init(fstream);
var data = sstream.read(100);
sstream.close();

Further information: https://developer.mozilla.org/en/Code_snippets/File_I%2F%2FO

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号