Warning: This section describes the
File is non-standard, not generally compiled into distributions, is a potential source of huge security holes, and not well tested.
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
Here is the original proposal for this object, and a status update from December 1998: http://www.mozilla.org/js/js-file-object.html
new File(); new File(filename);
- Name of the file we want to work with. Directories and pipelines are considered special files. Pipelines either begin or end with the pipe (
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.
- A File object that represents the standard input (stdin).
- A File object that represents the standard output (stdout).
- A File object that represents the standard error (stderr).
- A File object referring to the current directory; this property may be set.
- The system name separator (slash on UNIX).
- The length of the file in bytes, or the number of entries in the directory. Note: calculating the length of the directory is very expensive.
- The directory containing the file.
- The canonical path to the file.
- The name of the file.
- True if the file is a directory.
- True if the file is a file.
- True if the file exists.
- True if the file can be read.
- True if the file can be written.
- True if the file is in append mode.
- True if the file is in replace mode.
- True if the file is open.
- A string specifying the type of data or encoding contained in the file. Currently
"unicode"(UCS-2). (XXX Note -- ASCII might imply ASCIIZ)
- A string describing the mode used to open the file.
Dateobject representing the time when the file was created.
Dateobject representing the time when the file was last modified.
- The length of the file in bytes; undefined(?) for directories and pipelines.
- True if random access is supported for this file. Binary (UTF-8) files and pipelines (including
File.intput) do not support random access.
- Force a flush on each line break? (readonly)
- The file position. Writing to the property moves the file pointer.
- True if the file is backed by a native stdio FILE stream. (i.e. file.input, file.output, file.error or a pipeline)
- Opens the file, specifying file mode and type.
- Closes the file.
- Removes the file, provided it is not open.
- Creates a verbatim copy of the file at a new location.
- Removes the file.
- Flushes the operating system's write buffers for the file and blocks until any pending data has been committed to disk.
- Moves the file position pointer forward or backwards.
- Read a fixed number of bytes from the file.
- Read line from the file.
- Read the entire file in, returning an array of lines.
- Write bytes to the file.
- Write bytes to the file, followed by a line separator, flushing the buffer if file.
- Writes an array of lines to the file, obeying the same semantics as
- Get a list of files, potentially matching a regular expression filter, from the file. Only has meaning when the file is a directory.
- Create a directory. Directory will be created in the same directory as the file, unless the file is a directory, in which case, the directory will be created in that directory. XXX
- Returns the canonical path to the file.
- Returns a file:// URL describing the file relative to the local file system. URL is URI-encoded.
Example: Hello, world
Example: Writing a new file
var file = new File("myfile.txt"); file.open("write,create", "text"); file.writeln("The quick brown fox jumped over the lazy dogs"); file.close();
Example: Reading from a file
var data; var file = new File("myfile.txt"); file.open("read", "text"); data = file.readln(); file.close();
Example: Sending mail through a pipeline