File Object

Top  Previous  Next

Manage files on flash storage.

Description

There is both a global static File object and an instance File object that represents an open file or folder. For example, you open a file using the static File.open() function:

 

myFile = File.open(...);

 

Then you can operate on the resulting instance File object:

 

len = myFile.length;

myFile.write(data);

myFile.close();

 

An instance File object can refer to an open file or a folder. An open folder can be used as the starting point for finding another file or folder (similar to a "current directory" -- see below).

 

File Paths

The static functions require you to specify a file or folder with a name string and optionally a folder. The name string may include folder names separated by '/' or '\' characters (note that '\' is the string escape character and must be written as '\\' in a literal string). To specify the root folder, use '/'.

 

The folder may be omitted or null if the name string specifies a file or folder relative the root folder. Or you can provide an instance File object that is an open folder and the name string will be relative that folder. For example:

 

dataFolder = File.open("data", File.openFolder | File.openAlways);

myDataFile = File.open("myFolder/myData.dat", File.openFile | File.createAlways, dataFolder);

// myDataFile is opened at \data\myFolder\myData.dat

dataFolder.close(); // myDataFile still open

Constants

createAlways

Specifies that an empty file should always be created.

createNew

Specifies that a file or folder should not be opened if it already exists.

openAlways

Specifies that the file or folder should be created if it does not exist.

openExisting

Specifies that the file or folder must already exist.

openFile

Specifies that the operation should be performed on a file.

openFolder

Specifies that the operation should be performed on a folder.

openHidden

Specifies that hidden files or folders should be included in the operation.

Static Functions

enumStart

Starts enumeration of the contents of a folder.

move

Renames a file or folder, or moves it to a different folder.

open

Opens or creates a file or folder.

remove

Deletes a file or folder.

Instance Methods

close

Closes the file.

enumNext

Gets the next file in the enumeration of a folder.

read

Reads bytes from the file.

unzip

Unzips the contents of a ZIP compressed file.

write

Writes strings to the file.

writeBytes

Writes bytes to the file.

Instance Properties

dateModified

Gets or sets the date & time the file was modified.

isFolder

Gets a Boolean indicating whether the File object is a folder or file.

length

Gets or sets the length of the file.

position

Gets or sets the current read/write position in the file.