Interface File
access this type via: io.File (provides, requires or uses)
--- semantic variants: <default>, mem
Create, read and write files, typically stored on a hard disk. See the FileSystem API for further meta-operations relating to files.
Constants
int READ
int FILE_ACCESS_READ
int WRITE
int FILE_ACCESS_WRITE
int CREATE
int FILE_CREATE
Functions
File(char fn[], byte accessType)
byte[] read(int length)
int write(byte content[])
bool flush()
bool setPos(int pos)
int getPos()
bool eof()
int getSize()
void close()
Constants
READ Flag to open a file in read mode, with the read head positioned at the start of the file. The file will not be opened if it does not exist.
FILE_ACCESS_READ Synonym for READ, for backwards compatibility.
WRITE Flag to open a file in write-append mode, with the read/write head positioned at the start of the file. The file will be created if it does not exist; if it does exist its contents will be unchanged by the act of opening the file.
FILE_ACCESS_WRITE Synonym for WRITE, for backwards compatibility.
CREATE Flag to open a file in write mode. The file will be created if it does not exist; if it does exist its contents will be erased.
FILE_CREATE Synonym for CREATE, for backwards compatibility.
File(char fn[], byte accessType)
Open a file in a chosen mode, one of the file mode constants such as READ or WRITE.
byte[] read(int length)
Read a given number of bytes from a file.
returns: The number of bytes requested, or less than this if the end of the file is reached before length bytes.
int write(byte content[])
Write an array of bytes to a file.
returns: The number of bytes actually written to the file (returning a number less that content.arrayLength indicates that an error occurred).
bool flush()
Flush any buffered bytes from write operations to disk.
returns: True if successful, false otherwise.
bool setPos(int pos)
Move the logical read/write position of the file to a specified offset.
returns: True if the operation succeeded, false otherwise (for example if the requested position is larger than the size of the file).
int getPos()
Get the current read/write position of the file. This offset is the location at which the next bytes will be read or written by read() or write().
returns: The current read/write position of the file.
bool eof()
Check if the end of the file has been reached. This only makes sense when a file has been opened in read mode.
returns: True if the end of the file has been reached, false otherwise.
int getSize()
Get the size of the file in bytes.
returns: The current size of the file, in bytes.
void close()
Close the file. Note that, for files open in write mode, the contents of the file may not reflect everything that has been written using write() if close is not called.