JSFileApi: Difference between revisions

From MozillaWiki
Jump to navigation Jump to search
No edit summary
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 = "directory"; // "directory" on unix
  var subdir = new File(dir, "name.txt") // /path/name.txt on unix
  var file = File.path.join(dir, "name.txt") // /path/name.txt on unix


  File.path is the relative path
  File.path.abspath(file)
File.absolutePath()
  File.path.realpath(file)
  File.resolveLinks()
  File.stat(file) returns an object describing the file eg {isDirectory:true, isFile:false, lastModified, ...}  
  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)
  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 [file,stat_info] in file.readdir() {
  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.  
  // here type is a directory|file.
  i = file.stat()
  // i = { size: 1, lastModified:...}
  }
  }


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()