File I/O
Use this API to access local files for reading and writing. Only string data can be read and written with this API. Please note that binary files are not supported. Strings filled with \0 will be truncated to the last non \0 character.
This API is part of the coreapi
extension that is included automatically.
extensions: ["coreapi"]
Be sure to review the JavaScript API Usage guide for important information about using this API in JavaScript.
Be sure to review the Ruby API Usage guide for important information about using this API in Ruby.
Return basename part for the specified path. Will remove any prefix up to the last path separator (‘/’ or ‘\’ depending on platform). Example: for path ‘/full/path/to/file.ext’ basename will return ‘file.ext’.
Parameters
Absolute path for which basename is given.
Synchronous Return:
Basename part for the given path.
Method Access:
Rho.RhoFile.basename(STRING path)
Rho::RhoFile.basename(STRING path)
Closes file.
Synchronous Return:
Method Access:
myObj.close()
@myObj.close()
Copies file from “from” (source) to “to” (destination).
Parameters
File name to be copied.
Destination path.
Synchronous Return:
Error code raised during copy operation. 0 if no error.
Method Access:
Rho.RhoFile.copy(STRING from, STRING to)
Rho::RhoFile.copy(STRING from, STRING to)
Deletes specified directory. The specified directory must be empty to be deleted.
Parameters
Absolute path to the directory to be deleted.
Synchronous Return:
Error code raised on directory deletion. 0 on success, -1 otherwise.
Method Access:
Rho.RhoFile.deleteDir(STRING path)
Rho::RhoFile.deleteDir(STRING path)
Deletes file.
Parameters
Absolute path to the file to be deleted.
Synchronous Return:
Error code raised on file deletion. 0 on success, -1 otherwise.
Method Access:
Rho.RhoFile.deleteFile(STRING path)
Rho::RhoFile.deleteFile(STRING path)
Deletes all directory contents.
Parameters
Absolute path to the directory.
Set to true if you only want to delete directory contents, but no the directory itself.
Synchronous Return:
Method Access:
Rho.RhoFile.deleteRecursive(STRING path, BOOLEAN leaveRoot)
Rho::RhoFile.deleteRecursive(STRING path, BOOLEAN leaveRoot)
Returns directory name part of the specified path.
Parameters
Absolute path to directory.
Synchronous Return:
Directory name.
Method Access:
Rho.RhoFile.dirname(STRING path)
Rho::RhoFile.dirname(STRING path)
Checks if specified path exists.
Parameters
Absolute path to file or directory.
Synchronous Return:
True if path exists, otherwise false.
Method Access:
Rho.RhoFile.exists(STRING path)
Rho::RhoFile.exists(STRING path)
Flushes all buffered data to the storage media.
Synchronous Return:
Method Access:
myObject.flush()
Returns size of the file.
Parameters
Absolute path to the file.
Synchronous Return:
File size in bytes.
Method Access:
Rho.RhoFile.getFileSize(STRING path)
Rho::RhoFile.getFileSize(STRING path)
Checks if specified path is a directory.
Parameters
Absolute path of the directory.
Synchronous Return:
True if path is a directory. False otherwise.
Method Access:
Rho.RhoFile.isDir(STRING path)
Rho::RhoFile.isDir(STRING path)
Checks if specified path is a file.
Parameters
Absolute path of the file.
Synchronous Return:
True if path is a file, false otherwise.
Method Access:
Rho.RhoFile.isFile(STRING path)
Rho::RhoFile.isFile(STRING path)
Checks if file is opened.
Synchronous Return:
True if file is opened, false otherwise.
Method Access:
myObject.isOpened()
Joins two parts of the path considering all necessary path delimiters. I.e.: join(‘/path/to’,‘file’) will return ‘/path/to/file’
Parameters
First part of the path.
Second part of the path.
Synchronous Return:
Joined path.
Method Access:
Rho.RhoFile.join(STRING p1, STRING p2)
Rho::RhoFile.join(STRING p1, STRING p2)
Lists all entries of specified directory.
Parameters
Absolute path to directory.
Synchronous Return:
Array of directory entry names. If directory is empty array of size 0 will be returned.
Method Access:
Rho.RhoFile.listDir(STRING path)
Rho::RhoFile.listDir(STRING path)
Creates directory with the provided path. If all top-level nodes of the path doesn’t exists, directory will not be created.
Parameters
Path to the new directory.
Synchronous Return:
Error code raised on directory creation. 0 on success, -1 otherwise.
Method Access:
Rho.RhoFile.makeDir(STRING path)
Rho::RhoFile.makeDir(STRING path)
Creates directory and all top-level directories if necessary. Subsequent isDir call is necessary to check if path was successfully created.
Parameters
Absolute path to the directory to be created.
Synchronous Return:
Method Access:
Rho.RhoFile.makeDirs(STRING path)
Rho::RhoFile.makeDirs(STRING path)
Opens file in a mode specified by the “mode” parameter.
Parameters
Absolute path to the file.
Mode in which to open the file.
Possible Values :
Open file for output at the end of a file. The file is created if it does not exist.
Open file for read-only operations. The file must exist.
Create an empty file for output operations. If a file with the same name already exists, its contents are discarded and the file is treated as a new empty file.
Open a file for update (both for read and write). The file must exist.
Synchronous Return:
Method Access:
var myObj = new Rho.RhoFile(STRING path, INTEGER mode)
@myObj = Rho::RhoFile.new(STRING path, INTEGER mode)
Reads specified file to a string object.
Parameters
Absolute path to file.
Synchronous Return:
File contents.
Method Access:
Rho.RhoFile.read(STRING path)
Rho::RhoFile.read(STRING path)
Reads specified number of characters from current position of the file.
Parameters
Number of characters to be read.
Synchronous Return:
String from current position of file containing specified number of characters.
Method Access:
myObject.read(INTEGER size)
Reads all file contents to the string object.
Synchronous Return:
Contents of the file.
Method Access:
myObject.readAll()
Renames / moves file.
Parameters
Original path / name.
New path / name.
Synchronous Return:
Error code raised on move / rename. 0 on success, -1 otherwise.
Method Access:
Rho.RhoFile.rename(STRING from, STRING to)
Rho::RhoFile.rename(STRING from, STRING to)
Sets file position to specified value from the beginning of the file.
Parameters
Position within the file.
Synchronous Return:
Method Access:
myObject.seek(INTEGER pos)
Returns file size.
Synchronous Return:
File size in bytes.
Method Access:
myObject.size()
Writes the provided string at current position in the file.
Parameters
String to be written to the file.
Synchronous Return:
Number of bytes written.
Method Access:
myObject.write(STRING val)
Iterate through the contents of a folder and divide the entries in “folders” and “files”.
var folders = []; var files = []; var root_path = "/arbitrary/path" var entries = Rho.RhoFile.listDir(root_path); for (var i = 0; i < entries.length; i++) { var entry = entries[i]; if ((entry != ".") && (entry != "..")) { (Rho.RhoFile.isDir(Rho.RhoFile.join(root_path, entry)) ? folders : files).push(entry); } }
folders = [] files = [] root_path = "/arbitrary/path" entries = Rho::File.listDir(root_path) entries.each do |entry| unless (entry == "." || entry == "..") (Rho::File.isDir(Rho::File.join(root_path,entry)) ? folders : files) << entry end end