Class: FileApi

FileApi

The FileApi class provides specialized API calls to interact with the file system.

See also


new FileApi()

Developers never istantiate the FileApi class: this is done internally by the SmartFoxServer 2X API; get a reference to it using the Extension's getFileApi method.

Methods


copyFile(srcPath, destPath)

Copies a file to a new location.

This method copies the contents of the specified source path to the specified destination path. The folder holding the destination file is created if it does not exist. If the destination file exists, then this method will overwrite it.

Parameters:
Name Type Description
srcPath string The path to an existing file to copy.
destPath string The path of the destination file.
Throws:
An IOException Java exception in case the source or destination is invalid, or an IO error occurs during copying.

deleteDirectory(dirPath)

Deletes a folder including all its sub-folders, never throwing an exception.
Parameters:
Name Type Description
dirPath string The path of the folder to delete.
Returns:
true if the folder was deleted, false otherwise.
Type
boolean

deleteFile(filePath)

Deletes a file, never throwing an exception.
Parameters:
Name Type Description
filePath string The path of the file to delete.
Returns:
true if the file was deleted, false otherwise.
Type
boolean

getCurrentFolder()

Returns the relative path to the current Extension folder.

Typically this method will return a string like "extensions/{name-of-extension}/". The path is relative to the server root folder and it can be used to load external data files that are stored together with the Extension's JavaScript file(s).

Returns:
The path of the current Extension folder.
Type
string

getFileSize(filePath)

Returns the length of the file denoted by the passed path.
Parameters:
Name Type Description
filePath string The path of the file to get the size of.
Returns:
The length, in bytes, of the file denoted by the passed path.
Type
number

isDirectory(filePath)

Tests whether the file denoted by the passed path is a directory.
Parameters:
Name Type Description
filePath string The path of the file to check.
Returns:
true if and only if the file denoted by the passed path exists and is directory; false otherwise.
Type
boolean

isFile(filePath)

Tests whether the file denoted by the passed path is a normal file.
Parameters:
Name Type Description
filePath string The path of the file to check.
Returns:
true if and only if the file denoted by the passed path exists and is a normal file; false otherwise.
Type
boolean

makeDirectory(fullPath)

Makes a directory, including any necessary but nonexistent parent directories.
Parameters:
Name Type Description
fullPath string The path of the direcotry to create.
Throws:
An IOException Java exception in case the directory cannot be created or the file already exists but is not a directory.

moveFile(srcPath, destPath)

Moves a file to a new location.
Parameters:
Name Type Description
srcPath string The path to an existing file to be moved.
destPath string The path of the destination file.
Throws:
  • An IOException Java exception in case the source or destination is invalid, or an IO error occurs during copying.
  • An FileExistsException Java exception in case the destination file already exists.

readBinaryFile(filePath)

Reads the contents of a file into an array of bytes. The file is always closed.
Parameters:
Name Type Description
filePath string The path of the file to read.
Throws:
An IOException Java exception in case of an I/O error.
Returns:
The file contents as a Java byte array (byte[]). The content of the array can be accessed using the methods of native JavaScript arrays.
Type
Array.<byte>

readTextFile(filePath)

Reads the contents of a file into a string using the default encoding for the Java Virtual Machine. The file is always closed.
Parameters:
Name Type Description
filePath string The path of the file to read.
Throws:
An IOException Java exception in case of an I/O error.
Returns:
The file contents.
Type
string

writeBinaryFile(filePath, data)

Writes a Java byte array (byte[]) to a file, creating the file if it does not exist.
Parameters:
Name Type Description
filePath string The path of the file to write.
data Array.<byte> The Java byte array to write to the file.
Throws:
An IOException Java exception in case of an I/O error.

writeTextFile(filePath, data)

Writes a string to a file using the default encoding for the Java Virtual Machine, creating the file if it does not exist.
Parameters:
Name Type Description
filePath string The path of the file to write.
data string The content to write to the file.
Throws:
An IOException Java exception in case of an I/O error.