Handling read/write files

Create folder

boolean fs.mkdir(strPath);

Creates a folder, if not already existing, in the specified path. Returns true on success and false if it fails.

Parameter Description
strPath Path string
Remove folder

boolean fs.rmdir(dirPath);

Remove directory at strPath if exists and empty. Returns true on success and false if it fails.

Parameter Description
dirPath Folder string
Read folder content

object fs.readdir(dirPath);

Reads the contents of a folder. Returns an array of the names of the files in the folder excluding '.' and '..'. Returns empty list if it fails.

Parameter Description
dirPath Folder string
Read file

object fs.readFile(strfile [,strFlag]);

Opens the strFile file in read mode, reads its contents and returns it.

Parameter Description
strFile File name string
strFlag Read file mode:
  •     “b”: reads and returns as binary file (otherwise returns a text file)

Write file

fs.writeFile(strFile, fileData, [strFlag]);

Creates the strFile file if not present. Opens the strFile file in write mode and writes the data fileData to the file.

Parameter Description
strFile File name string
fileData Data to be write on the file in byte array
strFlag

Write file mode:

  •     “a”: appends fileData to the end of the text file
  •     “r”: replaces the contents of the file with fileData
  •    “ab”: appends fileData to the end of the binary file
  •    “rb”: replaces the contents of the binary file with fileData

Default flag is for writing text file in append and write mode. File path will be created if not present.

Returns -1 if write error occurs.

Append file

int fs.appendFile(strFile, fileData);

If the files does not exist creates it, otherwise append to existing file. Returns the number of character written or -1 on error.

Parameter Description
strFile File name string
fileData

Data to be write on the file in byte array

File exists

boolean fs.exists(strPath)

Returns true if the file or folder exists at strPath.

Parameter Description
strPath Path string
Remove file

boolean fs.unlink(strPath)

Removes the given file at strPath from filesystem if exists. Returns true on success and false if it fails.

Parameter Description
strPath Path string
File status

object fs.stat(strPath)

Retrieves information on the file/folder present at the specified path.

Parameter Description
strPath File/folder path string

var fileStats = var fs.stat(strPath)

fileStats.isFile

True if path is a file

fileStats.isDir

True if path is a folder

fileStats.size

Size in bytes of that file

fileStats.atime

Date object representing the last read access time

fileStats.mtime Date object representing the last write access time
fileStats.ctime

Date object representing the creation time

fileStats.perm File permissions

If path is invalid both isFile and isDir fields return false.

File permission table
0x4000 File is readable by the owner of the file
0x2000 File is writable by the owner of the file
0x1000 File is executable by the owner of the file
0x0400 File is readable by the user
0x0200 File is writable by the user
0x0100 File is executable by the user
0x0040 File is readable by the group
0x0020 File is writable by the group
0x0010 File is executable by the group
0x0004 File is readable by anyone
0x0002 File is writable by anyone
Important notes on file handling

Path for files and folders are expected to be UNIX style. This means the backslash character (\) is not recognized. Use slash character (/) instead.

File system object is a client side object. So operations are performed on local file system, not on server file system.

Current JavaScript API to get access at the device file system has been designed to manipulate small files. When a file is read, the entire file contents is temporarily stored inside the RAM available for JavaScript environment (16MB) and an exception is raised when there is not enough available memory. Good programming practice is to include the fs.readFile() call inside a try/catch block.