open Function

Top  Previous  Next

Opens a file or folder.

Syntax [, flags [, folder]])


pathA string with the path to the file or folder to be opened.
flagsAn optional integer with flags indicating how to open the file and whether it can be a file or folder.
folderAn optional instance File object representing a folder. If present, path is relative to folder; otherwise, path is from the root.

Return Value

If successful, an instance File object; otherwise an integer with an error code.


Use the typeof operator to determine if the result is an object (success) or a number (failure).


The flags argument combines several constants of the File object by + (addition) or | (bitwise OR). First, either openFile, openFolder, or both must be specified. To that, include one of the following:



An empty file should always be created; if the file already exists, it is truncated to zero length.


A file or folder should be created only if it does not exist. If the file exists, the operation fails.


A file or folder should be opened if it already exists, or created if it does not.


A file or folder must already exist.


Finally, openHidden may be optionally included. If the flags argument is omitted, the value openFile | openFolder | openExisting is used.


The close method must be called when operations on the file are complete.


myFile ="data/myFile.dat", File.openFile | File.createAlways);

if (typeof myFile == "object")


   myFile.write("Writing text to the file");



See Also