FESI language extensions - File I/O
Author: Jean-MarcLugrin
Description: Describe the FESI EcmaScript interpreter extensions to access File I/O capabilities
Keywords: EcmaScript, JavaScript, JScript, Java, extension

| Free EcmaScript Interpreter. A JavaScript interpreter written in Java.

Language extensions - FileIO

The `FileIO` extension is always loaded by the interactive interpreter, and provide access to file functions. The FileIO mechanism is patterned after the Netscape File object (available on the server version of Javascript), but it has been adapted to be nearer of the Java File and Stream capabilities. Currently only text files are supported. The Microsoft JScript I/O mechanism is based on ActiveX and is very Windows specific, so it is not suitable as a general purpose model for a portable EcmaScript implementation.

A `FESI` File object correspond to a Java `File` object, possibly linked to a `Stream` (when the file is opened). The `File` object is used both for file operations (as delete or move) and for the I/O operations. Error can be detected by a special value being returned, and by the function `error`. The error text consists of the name of the exception (without the package prefix), followed by a colon, a space and a text. This is more portable than the error code used by Netscape.

### File object constructor

A `File` object is created by the function `File("pathname")`, `File("directory","filename")`, or the corresponding constructors. The directory can be a `File` object. The default value of a `File` object is its path name, represented as a string.

### File prototype functions

The following routines are defined on any `File` object (but may be effectively used only if the object is in a correct state), as they are implemented by the file prototype object:

`readAll()`
  Open the file, read all text, close the file and return the read text as a single string. Returns null in case of error. The file must not be opened when `readAll` is called!.
`open()`
  Open the file for reading if it exists, for writing if it does not exist. Return true if successful. To create a new file, just delete it (using `remove()`) before calling open..
`close()`
  Close a file, return true if successful.
`isopened()`
  Return true if the file is opened (even if at EOF), false otherwise..
`write(...)`
  Write the parameters as a string to the file, return true if successful.
`writeln(...)`
  Write the parameters as a string to the file, followed by a new line, return true if successful.
`flush()`
  Flush the output buffer on a file opened for writing, return true in case of success..
`readln()`
  Read a line from an input file. Return `null` if at eof or in case of error. It may be more convenient to use the `error()` routine to check for end of file or error conditions.
`eof()`
  Return true of the file is at eof or if it is not open for reading. An error is set if the file was not opened for reading.
`exists()`
  Return true if the file exists (whether opened or not).
`isFile()`
  Return true if the file exists and is a regular file (not a directory).
`isDirectory()`
  Return true if the file exists and is a directory.
`list()`
  Return the list of files or directories in a directory, as an array of strings, but returns `null` if the `File` object does not represent a directory.
`mkdir()`
  Create the directory or directories specified by the path of the File object if they do not exist. Return true if thedirectories were successfully created, false otherwise..
`getLength()`
  Return the length of the file, 0 if it does not exist..
`lastModified()`
  Return the date and time when the file was last modified, Jan 1 1970 if it does not exist.
`remove()`
  Delete the file, return true in case of success. The name `remove` is used as `delete` is an EcmaScript keyword..
`renameTo("new name")`
  Rename the file to the target name, which can be a File object or a string, return true if successful.
`canRead()`
  Return true if the file exists and is readable.
`canWrite()`
  Return true if the file exists and is can be written to
`getPath()`
  Return the path given at construction time. Same as `toString()`.
`getAbsolutePath()`
  Get the complete path of the file.
`isAbsolute()`
  Return true if the path is absolute, false otherwise.
`getName()`
  Get the file component part of the path.
`getParent()`
  Get the directory part of the file or the parent directory of a directory . Returns the null string if applied to the root directory (of any drive in Windows).
`error()`
  Return the string of the last error on this file object since it was created or since clearError was called. Return the empty string (which is equivalent to false) if not error was detected.
`clearError()`
  Clear the error indicator.
`toString()`
  Return the path given at construction time. Same as `getPath()`.
`valueOf()`
  TBD.

### File object properties

The `File` object itself has the following attributes:

`separator`
  The path separator, for example the backslash for Windows.
`stdin`
  The standard input.
`stdout`
  The standard output.
`stderr()`
  The standard error.

The standard IO streams cannot be renamed or closed. Some other operations may be impossible or ignored.

Return to the main page

Last update: 17 June 1998