File

File handling class.

File.read("foo.txt", {encoding: "utf8"}, function(err, buffer) {
    // buffer is an UTF8 string containing the data
    console.log( buffer );
});

new File(path[, options], callback)

Constructor for a File object that can do operations on files.

Arguments:
path (String)

File path relative to the NML (Frontend) or the current working directory (Server)

options (Object)

Options object (encoding)

encoding (String)

encoding string (e.g. 'utf8')

callback (Function)

Read callback function

err (String)

Error description or null

buffer

(String | ArrayBuffer): The file content

close()

Closes an open file.

closeSync() Slow Method

Close a file in a synchronous way.

isDir()

Determines if the file is a directory or a file.

The file needs to be opened before calling this method.

Returns: Boolean

Returns true if the filename exists and is a directory, false otherwise

listFiles()

List the files in a directory.

The file needs to be opened before calling this method.

open(mode, callback)

Opens a file.

Arguments:
mode (String)

The mode parameter specifies the type of access you require to the file.

| Mode | Description |

| --- | --- |

| r | Open text file for reading. The stream is positioned at the beginning of the file. |

| r+ | Open for reading and writing. The stream is positioned at the beginning of the file. |

| w | Truncate to zero length or create text file for writing. The stream is positioned at the beginning of the file. |

| w+ | Open for reading and writing. The file is created if it does not exist, otherwise it is truncated. The stream is positioned at the beginning of the file. |

| a | Open for writing. The file is created if it does not exist. The stream is positioned at the end of the file. Subsequent writes to the file will always end up at the then current end of file, irrespective of any intervening fseek(3) or similar. |

| a+ | Open for reading and writing. The file is created if it does not exist. The stream is positioned at the end of the file. Subsequent writes to the file will always end up at the then current end of file, irrespective of any intervening fseek(3) or similar. |

callback (Function)

The callback function

Error (String)

Error description

openSync() Slow Method

Open a file in a synchronous way.

read(bytes=0, callback)

Reads from a file.

Arguments:
bytes (Integer)

Number of bytes to read

callback (Function)

Read callback function

err (String)

Error description

buffer (String)

The file content

readSync([readSize=T]) Slow Method

Read a file in a synchronous way.

If the file has not been opened it will be automatically opened.

Arguments:
readSize (Integer)

Number of bytes to read

Returns: String | ArrayBuffer

The file's content

rm()

Remove a file.

rmrf()

Deletes the directory and it's content.

seek(offset=0, callback)

Moves to a certain offset from the beginning of the file.

Arguments:
offset (Integer)

Move to the offset from the beginning of the file. The offset is expressed in bytes.

callback (Function)

Read callback function

err (String)

Error description

seekSync(offset=0)

Moves to a certain offset from the beginning of the file.

Arguments:
offset (Integer)

Move to the offset from the beginning of the file. The offset is expressed in bytes.

write(buffer, callback)

Writes a string or arraybuffer to a file.

Arguments:
buffer

(String | ArrayBuffer): The content to write to the file

callback (Function)

Read callback function

err (String)

Error description

writeSync(buffer) Slow Method

Writes a string or arraybuffer to a file in a synchronous way.

Arguments:
buffer

(String | ArrayBuffer): The content to write to the file

read(path[, options], callback) (static)

Reads entire file.

This method also accept http file or any protocol supported by Nidium.

Arguments:
path (String)

File path relative to the NML (Frontend) or the current working directory (Server)

options (Object)

Options object (encoding)

encoding (String)

encoding string (e.g. 'utf8')

callback (Function)

Read callback function

err (String)

Error description

buffer (String)

The file content

Returns: String

The file's content

readSync(path[, options]) (static) Slow Method

Reads entire file in a synchronous way.

Arguments:
path (String)

File path relative to the NML (Frontend) or the current working directory (Server)

options (Object)

Options object

encoding (String)

encoding string (e.g. 'utf8')

Returns: String

The file's content

filename(readonly)

  • Type: String

The name of the file.

var f = new File("foobar");
console.log(f.filename);

filesize(readonly)

  • Type: String

The size of the file in bytes.

File.read("tmp_0.txt", {encoding: "utf8"}, function(err, buffer) {
    // buffer is an UTF8 string containing the data
    console.log(this.filename + " " + this.filesize + " bytes");
});

Caught a mistake or want to contribute to the documentation? Edit this page on GitHub!