JSFileApi: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
var dir = | var dir = "directory"; // "directory" on unix | ||
var | var file = File.path.join(dir, "name.txt") // /path/name.txt on unix | ||
File.path | File.path.abspath(file) | ||
File.path.realpath(file) | |||
File. | File.stat(file) returns an object describing the file eg {isDirectory:true, isFile:false, lastModified, ...} | ||
File.stat() returns | |||
File.readdir() returns an array of arrays(because we don't have tuples) | File.readdir(dir) returns an array of arrays(because we don't have tuples) | ||
//readdir throws an exception if it isn't a directory | //readdir throws an exception if it isn't a directory | ||
for each [ | 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. | // 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. | ||
} | } | ||
Line 22: | Line 17: | ||
File.open should be similar to the unix open. | File.open should be similar to the unix open. | ||
var desc = File.open([File.WRITE_ONLY, File.CREATE]) | var desc = File.open(path, [File.WRITE_ONLY, File.CREATE]) | ||
desc.write("foo"); | desc.write("foo"); | ||
desc.close() | desc.close() | ||
var d2 = File.open([File.READ]) | var d2 = File.open(path, [File.READ]) | ||
d2.seek(...) | d2.seek(...) | ||
var contents = d2.read()// with optional size parameter to read less than full remaining file | var contents = d2.read()// with optional size parameter to read less than full remaining file | ||
d2.close() | d2.close() |
Revision as of 21:58, 19 May 2010
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()