Warning: This section describes the File component of the SpiderMonkey JavaScript interpreter. File is non-standard, not generally compiled into distributions, is a potential source of huge security holes, and not well tested.
In order to use the File object from your JavaScript programs, you must enable it by setting the make variable JS_HAS_FILE_OBJECT during the compilation of your Spidermonkey engine.
If you are building a standalone version of Spidermonkey (see: SpiderMonkey Build Documentation), this variable can be added on the make command line, like so:
cd mozilla/js/src make -f Makefile.ref JS_HAS_FILE_OBJECT=1
Alternatively, if you are building a larger product (such as a browser) and want to include the File object, you may need to perform minor Makefile surgery.
Non-Standard Server-Side Object
This object lets you work files and directories on the local filesystem, and create OS pipelines. Creating a pipeline involves spawning arbitrary processes; this means that giving a script access to the File object is exactly equivalent to giving the script access to the UNIX shell or DOS command interpreter.
Filesystem access is implemented with NSPR I/O Functions, and as such shares many semantics. Pipelines are implemented with the popen() system call. There is currently no support for p2open()-like semantics.
Here is the original proposal for this object, and a status update from December 1998: http://www.mozilla.org/js/js-file-object.html
The File constructor:
new File(); new File(filename);
filename|) symbol.Filenames are specified as strings that have an implementation defined format. Use of standard "file:" URLs is encouraged. If no argument is supplied to the constructor, the current working directory is the file object that is returned.
Examples of possible prefix strings are "/" to indicate the Unix root directory, "c:" to specify a Windows drive letter, and "file:" to indicate a file URL.
When a file is constructed, leading and trailing spaces are removed from the filename, so new File(" abc.txt ") just creates a file called abc.txt. Filenames starting and ending with the pipe symbol (|) are interpreted as pipes. Readable pipelines (i.e. pipe to programs generating output on stdout) begin with the pipe symbol; writeable pipelines end with the pipe symbol. Bi-directional pipelines are not supported.
File.inputFile.currentDirFile.currentDirFile.currentDirFile.separatorFile.lengthFile.parentFile.pathFile.nameFile.isDirectoryFile.isFileFile.existsFile.canReadFile.canWriteFile.canAppendFile.canReplaceFile.isOpenFile.type"ascii" (ASCII), "binary" (UTF-8) or "unicode" (UCS-2). (XXX Note -- ASCII might imply ASCIIZ)File.modeFile.creationTimeDate object representing the time when the file was created.File.lastModifiedDate object representing the time when the file was last modified.File.sizeFile.hasRandomAccessFile.intput) do not support random access.File.hasAutoFlushFile.positionFile.isNativeFile.open()File.close()File.remove()File.copyTo()File.renameTo()File.flush()File.seek()File.read()File.readln()File.readAll()File.write()File.writeln()hasAutoflush.File.writeAll()writeln.File.list()File.mkdir()File.toString()File.toURL()File.output.writeln("Hello, world");
var file = new File("myfile.txt");
file.open("write,create", "text");
file.writeln("The quick brown fox jumped over the lazy dogs");
file.close();
var data;
var file = new File("myfile.txt");
file.open("read", "text");
data = file.readln();
file.close();
var mail = new File("|/usr/lib/sendmail foo@bar.com");
mail.writeln("I love JavaScript.\nPipe support is especially good!");
mail.close();