File read/write capability for text/string based files.

Methods

MacGap.File.write(filePath, fileData, fileType)

Arguments

filePath must be a string

fileData must be a string, JavaScript Object, or base64 representation of an image

fileType must be a string and contain one of the following values: string, json or image

Description

Write the provided fileData string to a file.

If fileType is set to json, this method will automatically convert the JavaScript object to proper JSON before writing to the file.

Example:
MacGap.File.write('path/to/file1', 'Data to save', 'string');
MacGap.File.write('path/to/file2', '{something:"else"}', 'json');

MacGap.File.read(filePath, fileType)

Arguments

filePath must be a string

fileType must be a string and contain one of the following values: string, json or image

Description

Read the file at the provided path. If fileType is set to json, this method will automatically convert the JSON back to a JavaScript object.

Example:
var fileDataString = MacGap.File.read('/Users/myname/Desktop/textfile.txt', 'string');

MacGap.File.exists(filePath)

Arguments

filePath must be a string

Description

Return a boolean indicating whether a file or folder exists at the provided filePath.

Note: checking if a file exists before opening it is an anti-pattern that leaves you vulnerable to race conditions: another process may remove the file between the calls to File.exists() and File.read(). Just read the file and handle the error when it's not there.

Example:
var tempDirExists = MacGap.File.exists('/tmp');