v1.2.0.1-beta1
|
The File class provides access to... files. Shocking.
It has many static functions which are accessed in JS with the File.
qualifier. These are "atomic" operations, eg. read a whole file at once or check if a file name exists, etc.
/
directory separator (recommended) as well as \
(which needs to be escaped as "\\"" per JS syntax rules). ArrayBuffer read | ( | String & | file, |
::FS::OpenMode | mode = O_TEXT |
||
) |
Reads a file and returns the contents as a byte array. Set mode
to FS.O_BIN
to read in binary mode. Default is to read as text.
ReferenceError | is thrown if file loading fails (file not found/etc) and returns and empty string. |
ArrayBuffer read | ( | String & | file, |
String & | mode | ||
) |
Reads a file and returns the contents as a byte array. Set mode
to 'b' to read in binary mode. Default is to read as text.
ReferenceError | is thrown if file loading fails (file not found/etc) and returns and empty string. |
Reads a file in text mode and returns the contents as a string. This is a convenience method which is equivalent to calling String(File.read(file));
ReferenceError | is thrown if file loading fails (file not found/etc) and returns and empty string. |
String readLines | ( | String & | file, |
int | maxLines, | ||
int | fromLine = 0 , |
||
bool | trimTrailingNewlines = true |
||
) |
Reads up to maxLines
lines from file
starting at fromLine
(default = 0, start of file) and returns the contents as a string. Can search from start or end of file.
maxLines
can be 0
in which case all remaining lines in the file are returned. This is useful when fromLine != 0
to skip a number of lines and then return the rest.
fromLine
can be negative, in which case the file will be read from the end, backwards. In this case -1
means starting at the end of the file, -2
means to skip one line ("start at second-to-last line"), and so on.
Set trimTrailingNewlines
to true
(default) to trim/skip over any empty lines from the end of the file. When searching backwards, setting trimTrailingNewlines
to false
will count every newline from the end, including any potential newline at end of the last line of the file.
ReferenceError | is thrown if file loading fails (file not found/etc) and returns and empty string. |
int write | ( | String & | file, |
ArrayBuffer & | data, | ||
::FS::OpenMode | mode = O_TEXT |
||
) |
Writes data
to file
and returns the number of bytes written. Returns -1
on error. mode
flags can be a combination of FileLib::OpenModeFlag
enums:
FS.O_BIN
write in binary mode (default is to write as text).FS.O_APPEND
to append to file (default is to truncate/replace it).FS.O_EXCL
to fail if the file exists, or FS.O_NOCREAT
to to fail if the file doesn't already exist (both will return an error if the condition isn't met).Error | is thrown if the file writing fails for any reason. |
int write | ( | String & | file, |
ArrayBuffer & | data, | ||
String & | mode | ||
) |
Writes data
to file
and returns the number of bytes written. Returns -1
on error. mode
flags can be a combination of the following (in any order):
b
write in binary mode (default is to write as text).a
to append to file (default is to truncate/replace it).x
to fail if the file exists, or n
to to fail if the file doesn't already exist (both will return an error if the condition isn't met).Error | is thrown if the file writing fails for any reason. |
bool remove | ( | String & | file | ) |
Removes the file specified by file
. Returns true
if successful; otherwise returns false
.
Renames the from
file to a file specified in to
. Returns true
if successful; otherwise returns false
. If a file with the name to
already exists, rename()
returns false
.
Copies the from
file to a file specified in to
. Returns true
if successful; otherwise returns false
. If a file with the name to
already exists, copy()
returns false
.
Creates a link named linkName that points to the file fileName. What a link is depends on the underlying filesystem (be it a shortcut on Windows or a symbolic link on Unix). Returns true if successful; otherwise returns false.
bool exists | ( | String & | file | ) |
Returns tru
e if the file exists; otherwise returns false
.
bool isFile | ( | String & | path | ) |
Returns true
if this object points to a file or to a symbolic link to a file. Returns false
if the object points to something which isn't a file, such as a directory.
bool isDir | ( | String & | path | ) |
Returns true
if this object points to a directory or to a symbolic link to a directory; otherwise returns false
.
bool isReadable | ( | String & | file | ) |
Returns true
if the user can read the file; otherwise returns false
.
bool isWritable | ( | String & | file | ) |
Returns true
if the user can write to the file; otherwise returns false
.
bool isAbs | ( | String & | file | ) |
Returns true
if the file path name is absolute, otherwise returns false
if the path is relative.
bool isExec | ( | String & | file | ) |
Returns true
if the file is executable; otherwise returns false
.
Returns the file name, including the path (which may be absolute or relative).
Returns the base name of the file without the path. The base name consists of all characters in the file up to (but not including) the first '.' character.
Returns the complete base name of the file without the path. The full base name consists of all characters in the file up to (but not including) the last '.' character.
Returns the suffix (extension) of the file. The suffix consists of all characters in the file after (but not including) the last '.'.
Returns the "full" suffix (extension) of the file. The full suffix consists of all characters in the file after (but not including) the first '.'.
Returns the file's absolute path, including the file name (with extension).
Returns the file's path canonical path (excluding the file name), i.e. an absolute path without symbolic links or redundant "." or ".." elements.
Returns the canonical path including the file name, i.e. an absolute path without symbolic links or redundant "." or ".." elements.
uint size | ( | String & | file | ) |
Returns the file size in bytes. If the file does not exist or cannot be fetched, 0 is returned.
Returns the date and time when the file was created / born. If the file birth time is not available, this function returns an invalid Date object.
Returns the date and local time when the file was last modified. If the file is not available, this function returns an invalid Date object.
Returns the date and local time when the file was last accessed (read). If the file is not available, this function returns an invalid Date object.
Returns the date and time when the file metadata (status, eg. permissions) was changed. If the file is not available, this function returns an invalid Date object.
::FS::Permissions permissions | ( | String & | file | ) |
Returns the complete OR-ed together combination of FS.Permissions
for the file.
bool setPermissions | ( | String & | file, |
::FS::Permissions | p | ||
) |
Sets the permissions for file
to the FS.Permissions
flags specified in p
. Returns true
if successful, or false
if the permissions cannot be modified.