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,
- storageType - {Optional. Constant}. Tells if the file is located in internal storage or SD card. Possible values are,
- $m.DEVICE_MEMORY - Default.
- $m.SDCARD
- $m.CACHE_DIR
- level - {Optional. Constant}. Specifies the root directory in the storage where the file is located. Possible values are,
- $m.PACK_LEVEL - Default.
- $m.APP_LEVEL
- $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,
- code - {Number}. Possible codes are,
- -1 - Storage is not ready.
- 1 - File path is successfully read.
- 0 - Error in reading file path.
- result - {Boolean}. True, if the delete file operation is successful; false, otherwise.
- error - {Object}. Available when the code is 0. The error object contains,
- message - {String}. A short description of the error.
- 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,
- code - {Number}. Possible codes are,
- -1 - Storage is not ready.
- 1 - File path is successfully read.
- 0 - Error in reading file path.
- result - {Boolean}. True, if the write operation is successful. False, otherwise.
- error - {Object}. Available when the code is 0. The error object contains,
- message - {String}. A short description of the error.
- 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,
- code - {Number}. Possible codes are,
- -1 - Storage is not ready.
- 1 - File path is successfully read.
- 0 - Error in reading file path.
- result - {String}. The content of the file.
- error - {Object}. Available when the code is 0. The error object contains,
- message - {String}. A short description of the error.
- 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,
- code - {Number}. Possible codes are,
- -1 - Storage is not ready.
- 1 - File path is successfully read.
- 0 - Error in reading file path.
- result - {String}. The absolute path of the file.
- error - {Object}. Available when the code is 0. The error object contains,
- message - {String}. A short description of the error.
- 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,
- code - {Number}. Possible codes are,
- -1 - Storage is not ready.
- 1 - File path is successfully read.
- 0 - Error in reading file path.
- result - {Boolean}. True, if the delete file operation is successful; false, otherwise.
- error - {Object}. Available when the code is 0. The error object contains,
- message - {String}. A short description of the error.
- description - {String}. A detailed description of the error.