file

File

Creates a new mowbly File instance with the specified file parameters.

var myFile = $m.file("offline_orders.txt",

{"level": $m.APP_LEVEL, "storageType": $m.SDCARD});

Syntax

        $m.file(filePath [, options, fp_callback])

Parameters

filePath

string (required)

The path of the file. If options parameter is not provided, the path is considered relative to the pack folder.

options

object (optional)

Options for the mowbly file object. It can contain the following properties,

  1. storageType - {Optional. Constant}. Tells if the file is located in internal storage or SD card. Possible values are,
  1. $m.DEVICE_MEMORY - Default.
  2. $m.SDCARD
  3. $m.CACHE_DIR

  1. level - {Optional. Constant}. Specifies the root directory in the storage where the file is located. Possible values are,
  1. $m.PACK_LEVEL - Default.
  2. $m.APP_LEVEL
  3. $m.STORAGE_LEVEL

Remarks

The method does not create the file in native file system. Use file.create() method to create the file in the native file system.

Return value

file - {Object}. The mowbly file object with the specified file parameters. The object supports following methods,

exists

Checks if the file exists in the file system.

myFile.exists(function(response) {

        var message;

        if(response.code == -1) {

           // External storage is not ready. Possible reason could be the storage

           // is mounted to file system on a computer.

           return;

        }

        if(response.result) {

           message = "File existed ";

        } else {

           message = "Error - " + response.error.message;

        }

      $m.toast(message);

});

Syntax

        exists(fp_callback)

Parameters

fp_callback

function (optional)

Function to execute when the file exists check is done. The function receives a response object as parameter with the following properties,

  1. code - {Number}. Possible codes are,
  1. -1 -  Storage is not ready.
  2. 1  - File path is successfully read.
  3. 0  - Error in reading file path.

  1. result - {Boolean}. True, if the delete file operation is successful; false, otherwise.
  2. error - {Object}. Available when the code is 0. The error object contains,
  1. message - {String}. A short description of the error.
  2. description - {String}. A detailed description of the error.

write

Writes the string content to the file.

myFile.write("welcome to mowbly", function(response) {

      var message;

      if(response.code == -1) {

           // External storage is not ready. Possible reason could be the storage

           // is mounted to file system on a computer.

           return;

        }        

      if(response.result){

         message = "Successfully written to the file.";

        } else{

            message = response.error.message;

        }

      $m.toast(message);

});

Syntax

        write(content [, fp_callback])

Parameters

content

string (required)

The string content to be written to the file.

fp_callback

function (optional)

Function that is invoked when the file content is read. The function receives a response object as parameter with the following properties,

  1. code - {Number}. Possible codes are,
  1. -1 -  Storage is not ready.
  2. 1  - File path is successfully read.
  3. 0  - Error in reading file path.

  1. result - {Boolean}. True, if the write operation is successful. False, otherwise.

  1. error - {Object}. Available when the code is 0. The error object contains,
  1. message - {String}. A short description of the error.
  2. description - {String}. A detailed description of the error.

read

Returns the content of the file as string.

myFile.read(function(response) {

        if(response.code == -1) {

           // External storage is not ready. Possible reason could be the storage

           // is mounted to file system on a computer.

           return;

        }

      if(response.code){

           var fileContent = response.result;

        } else{

// show error as toast message.

                $m.toast(response.error.message);

        }

});

Syntax

        read([ fp_callback])

        

Parameters

fp_callback

function (optional)

Function that is invoked when the file content is read. The function receives a response object as parameter with the following properties,

  1. code - {Number}. Possible codes are,
  1. -1 -  Storage is not ready.
  2. 1  - File path is successfully read.
  3. 0  - Error in reading file path.

  1. result - {String}. The content of the file.
  1. error - {Object}. Available when the code is 0. The error object contains,
  1. message - {String}. A short description of the error.
  2. description - {String}. A detailed description of the error.

getPath

Returns the absolute file path of the file.

myFile.getPath(function(response) {

        var message;

        if(response.code == -1) {

           // External storage is not ready. Possible reason could be the storage

           // is mounted to file system on a computer.

           return;

        }

        if(response.result) {

           message = "File path is " + response.result;

        } else {

           message = "Error - " + response.error.message;

        }

      $m.toast(message);

});

Syntax

        getPath([fp_callback])

Parameters

fp_callback

function (optional)

Function that is invoked when the file path is read. The function receives a response object as parameter with the following properties,

  1. code - {Number}. Possible codes are,
  1. -1 -  Storage is not ready.
  2. 1  - File path is successfully read.
  3. 0  - Error in reading file path.

  1. result - {String}. The absolute path of the file.

  1. error - {Object}. Available when the code is 0. The error object contains,
  1. message - {String}. A short description of the error.
  2. description - {String}. A detailed description of the error.

remove

Deletes the file from the file system.

myFile.remove(function(response) {

    var message;

    if(response.code == -1) {

           // External storage is not ready. Possible reason could be the storage

           // is mounted to file system on a computer.

           return;

    }

    if(response.result) {

       message  = "File deleted successfully.";

    } else {

       message = "Error deleting file. " + response.error.message;

    }

    $m.toast(message);

});

Syntax

        remove([fp_callback])

Parameters

fp_callback

function (optional)

Function that is invoked when the delete file operation is done. The function receives a response object as parameter with the following properties,

 

  1. code - {Number}. Possible codes are,
  1. -1 -  Storage is not ready.
  2. 1  - File path is successfully read.
  3. 0  - Error in reading file path.

  1. result - {Boolean}. True, if the delete file operation is successful; false, otherwise.

  1. error - {Object}. Available when the code is 0. The error object contains,
  1. message - {String}. A short description of the error.
  2. description - {String}. A detailed description of the error.