JSFileApi

From MozillaWiki
Revision as of 21:58, 19 May 2010 by Tglek (talk | contribs)
Jump to navigation Jump to search
var dir = "directory"; // "directory" on unix
var file = File.path.join(dir, "name.txt") // /path/name.txt on unix
File.path.abspath(file)
File.path.realpath(file)
File.stat(file) returns an object describing the file eg {isDirectory:true, isFile:false, lastModified, ...} 
File.readdir(dir) returns an array of arrays(because we don't have tuples)
//readdir throws an exception if it isn't a directory
for each [filename, stat_info] in File.readdir(file) {
 // stat_info is akin to the info returned by stat(), but it only has the information returned by readdir(3) in it(ie is this a file/dir/etc), rest of the properties are lazily computed by doing a lazy stat() call. 
}


IO

File.open should be similar to the unix open.

var desc = File.open(path, [File.WRITE_ONLY, File.CREATE])
desc.write("foo");
desc.close()
var d2 = File.open(path, [File.READ])
d2.seek(...)
var contents = d2.read()// with optional size parameter to read less than full remaining file 
d2.close()