JSFileApi: Difference between revisions
Jump to navigation
Jump to search
(Created page with ' var dir = new File("/path"); // /path on unix var subdir = new File(dir, "name.txt") // /path/name.txt on unix //readdir throws an exception if it isn't a directory for ea…') |
No edit summary |
||
Line 1: | Line 1: | ||
File is an immutable object. To mutate it, just create new File()s. | |||
var dir = new File("/path"); // /path on unix | var dir = new File("/path"); // /path on unix | ||
var subdir = new File(dir, "name.txt") // /path/name.txt on unix | var subdir = new File(dir, "name.txt") // /path/name.txt on unix | ||
File.path is the relative path | |||
File.absolutePath() | |||
File.resolveLinks() | |||
File.stat() returns a new immutable objects describing the file eg {isDirectory:true, isFile:false, lastModified, ...} | |||
File.readdir() 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 [file, | for each [file,stat_info] in file.readdir() { | ||
// 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. | |||
// here type is a directory|file. | // here type is a directory|file. | ||
i = file.stat() | i = file.stat() | ||
// i = { size: 1, lastModified:...} | // i = { size: 1, lastModified:...} | ||
} | } | ||
== IO == | |||
File.open should be similar to the unix open. | |||
var desc = File.open([File.WRITE_ONLY, File.CREATE]) | |||
desc.write("foo"); | |||
desc.close() | |||
var d2 = File.open([File.READ]) | |||
d2.seek(...) | |||
var contents = d2.read()// with optional size parameter to read less than full remaining file | |||
d2.close() |
Revision as of 21:21, 19 May 2010
File is an immutable object. To mutate it, just create new File()s.
var dir = new File("/path"); // /path on unix var subdir = new File(dir, "name.txt") // /path/name.txt on unix
File.path is the relative path File.absolutePath() File.resolveLinks() File.stat() returns a new immutable objects describing the file eg {isDirectory:true, isFile:false, lastModified, ...}
File.readdir() returns an array of arrays(because we don't have tuples) //readdir throws an exception if it isn't a directory for each [file,stat_info] in file.readdir() { // 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. // here type is a directory|file. i = file.stat() // i = { size: 1, lastModified:...} }
IO
File.open should be similar to the unix open.
var desc = File.open([File.WRITE_ONLY, File.CREATE]) desc.write("foo"); desc.close() var d2 = File.open([File.READ]) d2.seek(...) var contents = d2.read()// with optional size parameter to read less than full remaining file d2.close()