V8 Javascript API

fs Class Reference

[File System]

Description

File system access functions. This module is based on the NodeJS module of the same name and provides similar functionality. Note that unlike NodeJS all functions are synchronous, so there are no Sync variants. Additionally all Strings are read and written using utf8 encoding.

Inner Classes

class 
More...
class 
More...
class 
Asynchronous variant of the fs api. More...

Static Functions

 close( fs.​File_descriptor fd)
Closes an open file. More...
 mkdir( String path)
Creates a directory inside content root. More...
static fs.​File_descriptor open( String filename, String flags)
Opens a file for reading or writing inside content root. More...
Integer read( fs.​File_descriptor fd, Uint8Array data, Integer offset, Integer length, Integer position)
Reads data from the file specified by fd. More...
Uint8Array readFile( String filename, Object options)
Reads the entire contents of a file from the filesystem inside content root. More...
 rmdir( String path)
Removes directory pointed to by path. More...
fs::​Stat stat( String path)
Returns file sytem information for a given path. More...
 unlink( String path)
Removes the file or symbolic link pointed to by path. More...
Integer write( fs.​File_descriptor fd, ArrayBuffer data, Integer offset, Integer length, Integer position)
Writes data to a file specified by fd. More...
Integer write( fs.​File_descriptor fd, String string, Integer position)
Writes a string to a file specified by fd. More...
 writeFile( String filename, ArrayBuffer data, Object options)
Writes a file to the filesystem inside content root. More...

Functions

fs.close( fs.​File_descriptor fd) [static]

Closes an open file.

Parameters

fd
the file descriptor to close.
fs.mkdir( String path) [static]

Creates a directory inside content root. All required parents will be created automatically.

Parameters

path
the path to create to. Must be relative and will be resolved within content root.
fs.​File_descriptor fs.open( String filename, String flags) [static]

Opens a file for reading or writing inside content root.

Parameters

filename
the filename to open. Must be a relative path and will be resolved within content root.
flags
mode to open in. One of:
  • 'r' : Open file for reading. An exception occurs if the file does not exist. (the default)
  • 'r+' : Open file for reading and writing. An exception occurs if the file does not exist.
  • 'w' : Open file for writing. The file is created (if it does not exist) or truncated (if it exists).
  • 'wx' : Like 'w' but fails if path exists.
  • 'w+' : Open file for reading and writing. The file is created (if it does not exist) or truncated (if it exists).
  • 'wx+': Like 'w+' but fails if path exists.
  • 'a' : Open file for appending. The file is created if it does not exist.
  • 'ax' : Like 'a' but fails if path exists.
  • 'a+' : Open file for reading and appending. The file is created if it does not exist.
  • 'ax+': Like 'a+' but fails if path exists.

Returns

The file descriptor, note this must be closed before the command returns otherwise the file descriptor is left dangling. Contains a fd property with the operating system file descriptor

Integer fs.read( fs.​File_descriptor fd, Uint8Array data, Integer offset, Integer length, Integer position) [static]

Reads data from the file specified by fd.

Parameters

fd
the file descriptor to read from
data
The data to read into. May be one of Uint8Array or Binary
offset
offset into data to start writing at. Optional, defaults to 0.
length
the number of bytes to read. Required if data is an empty Binary. Otherwise optional, and defaults to the data length - offset.
position
the position in the file to start reading from, if not a number, or < 0 then reads from the current position.

Returns

number of bytes read.

Uint8Array fs.readFile( String filename, Object options) [static]

Reads the entire contents of a file from the filesystem inside content root.

Parameters

filename
the filename to read. Must be relative and will be resolved within content root.
options
Object containing options
  • flag String : Mode in which to open the file. See open. Default 'r'
  • encoding String : If set then the file is read using the given encoding and a String returned. The only supported value is 'utf8'.

Returns

Returns Uint8Array if no encoding is given, otherwise a String.

fs.rmdir( String path) [static]

Removes directory pointed to by path.

Parameters

path
the path to the directory to remove. Must be relative and will be resolved within content root. Directory must be empty otherwise removal will fail
fs::​Stat fs.stat( String path) [static]

Returns file sytem information for a given path.

Parameters

path
the path to query. Must be relative and will be resolved within content root.

Returns

The file statistics.

fs.unlink( String path) [static]

Removes the file or symbolic link pointed to by path.

Parameters

path
the path to the file to remove. Must be relative and will be resolved within content root.
Integer fs.write( fs.​File_descriptor fd, ArrayBuffer data, Integer offset, Integer length, Integer position) [static]

Writes data to a file specified by fd.

Parameters

fd
the file descriptor to write to
data
The data to write to disk. May be one of the following:
  • ArrayBuffer
  • Uint8Array
  • Binary
offset
into the start of data to start writing from. Optional, defaults to 0.
length
the number of bytes in data to write. Optional, defaults to the entire buffer.
position
the position in the file to start writing at, if not a number, or < 0 then writes at current position.

Returns

number of bytes written.

Integer fs.write( fs.​File_descriptor fd, String string, Integer position) [static]

Writes a string to a file specified by fd.

Note:

Unlike when writing buffer, the entire string must be written. No substring may be specified. This is because the byte offset of the resulting data may not be the same as the string offset.

Note:

Strings are always written using utf8 encoding.

Parameters

fd
the file descriptor to write to
string
the string to write to disk.
position
the position in the file to start writing at, if not a number, or < 0 then writes at current position.

Returns

number of bytes written.

fs.writeFile( String filename, ArrayBuffer data, Object options) [static]

Writes a file to the filesystem inside content root.

Note:

Strings are always written using utf8 encoding.

Parameters

filename
the filename to write to. Must be relative and will be resolved within content root.
data
The data to write to disk. May be one of the following:
  • ArrayBuffer
  • Uint8Array
  • Binary
  • String
options
Object containing options
  • flag String : Mode in which to open the file. See open. Default 'w'