File API

FLX app provides a basic File API to find, create, read, write, and delete files similar to APIs provided in Android and Java/Kotlin.

File API Functions

Creates the file specified by the file if it does not exists.

  • file – a File object

Returns: a Boolean value indicating if the creation succeeded.

Returns the application’s data directory. This directory does not require any read nor write permissions from the user.

Returns: a File denoting the data directory.

Deletes the file denoted by given file. If the file is directory then all the files contained by the directory are deleted – so be careful.

  • file – a File object

Returns: a Boolean value indicating if the deleting succeeded.

Tests if the given file exists

  • file – a File object

Returns: a Boolean value.

Creates a new File instance from a directory and a fileName string.

  • directory – a File object specifying the directory
  • fileName – the name of the file to be created as a String

Returns: a File object or Null if file creation fails.

Creates a new File instance from a *filepath.

  • filepath – a String specifying the full file path for the file to be created

Returns: a File object or Null if file creation fails.

Creates a new File instance from a *directoryPath and a fileName.

  • directoryPath – a String specifying the full file path for the containing directory
  • fileName – the name of the file to be created as a String

Returns: a File object or Null if file creation fails.

Creates a new File instance from the given fileUri.

  • fileUri – A file URI specifying the file

Returns: a File object or Null if file creation fails.

Tests if the given file is a directory.

  • file – a File denoting the file to be tested

Returns: A Boolean value.

Tests if the given file is a plain file and not a directory.

  • file – a File denoting the file to be tested

Returns: A Boolean value.

Returns child files the given file. If the given file is not a directory, an empty array is returned.

  • file – a File

Returns: An Array of Files

Creates the non existing directories along the file path specified by the given file.

  • file – a File specifying a file path

Returns: A File if creation of directories succeeded, otherwise Null.

Reads the content of the given file to a JsonObject.

  • file – a File

Returns: a JsonObject. If reading fails a Null is returned.

Reads the content of the given file to a ´String´.

  • file – a File

Returns: A String. If reading fails a Null is returned.

Write the given string to the given file.

  • file – a File specifying the output file
  • string – a String to be written

Returns: a Boolean indicating if file writing succeeded.

Write the given json to the given file.

  • file – a File specifying the output file
  • json – a JsonObject to be written

Returns: a Boolean indicating if file writing succeeded.

Example

The usage of the functions of the File API is depicted in the following visual code example:

  1. dataDirectory function returns the application directory for writing and reading files.
  2. .isDirectory function tests if the given File is a directory. A similar function .isFile tests if a File is plain file and not a directory.
  3. .listFiles returns a list of child files (as an Array of Files) of the specified directory.
  4. File.new creates a new File object to represent a file inside the specified container directory.
  5. .write function writes the given JsonObject to a File. The function also supports writing any content as a String.
  6. .readJson reads file content of a given File as a JsonObject. There is also a .read function that reads the content of a file as a String.
  7. .delete removes the given File. It can be also used to recursively to delete a directory and all its contained files, so be careful with this function.
  8. .exists tests if the file specified by the given File exists.
  9. There is also .mkdirs function (not used in this example) that can used to create directories along the file path defined by a given File.