The bps.File class provides functionality for reading and writing binary and text files. More...
Public Types | |
enum | AccessMode { ReadOnly = 0x0001 , WriteOnly = 0x0002 , ReadWrite = 0x0003 , Append = 0x0004 , Truncate = 0x0008 , Text = 0x0010 , Unbuffered = 0x0020 } |
This enum is used with open() to describe the mode in which a device is opened. More... | |
Public Member Functions | |
void | close () |
Closes the file. | |
File (String aFileName) | |
Creates a file object with the with fileName. More... | |
void | open (AccessMode aAccessMode) |
Opens the file in the specified access mode if possible; otherwise throws an exception. More... | |
String | read () |
Returns the entire content of the file as a string if it can be read; otherwise throws an exception. More... | |
Number | readByte () |
Reads one byte from the file if possible; otherwise throws an exception. More... | |
String | readLine () |
Reads one line from file if possible; otherwise throws an exception. More... | |
Array | readLines () |
Read the contents of the file as an array of Strings, one for each line. More... | |
void | remove () |
Deletes the file if possible; otherwise throws an exception. More... | |
String | toString () const |
void | write (String aData) |
Writes data to the file if possible; otherwise throws an exception. More... | |
void | writeByte (Number aByte) |
Writes a byte to the file is possible; otherwise throws an exception. More... | |
void | writeLine (String aData) |
Writes the line data to the file and adds a linebreak. More... | |
Static Public Member Functions | |
static Boolean | exists (String aFileName) |
Check if file exists. More... | |
static Boolean | isDir (String aFileName) |
Check if aFileName is a directory. More... | |
static Boolean | isFile (String aFileName) |
Check if aFileName is a file. More... | |
static String | read (String aFileName) |
Reads and returns the contents of the file fileName if possible; otherwise throws an exception. More... | |
static void | remove (String aFileName) |
Deletes the file if possible; otherwise throws an exception. More... | |
static void | write (String aFileName, String aContent) |
Writes the string content to the file fileName if possible (completely replacing the original contents if the file already exists); otherwise throws an exception. More... | |
Properties | |
String | baseName |
The name of the file, excluding its path and extension. More... | |
Date | created |
The creation time of the file. More... | |
Number | defaultEncoding |
Default encoding used for static text read/write methods, and initial encoding for files opened subsequently in script context. More... | |
Number | encoding |
Encoding for the non-static text read and write methods. More... | |
Boolean | eof |
True if reading has reached the end of the file; otherwise false. More... | |
Boolean | executable |
True if the file is executable; otherwise false. More... | |
Boolean | exists |
True if the file exists; otherwise false. More... | |
String | extension |
The file name's extension. More... | |
String | fullName |
The fullName of the file, including path, name, and extension. More... | |
Boolean | hidden |
True if the file is hidden; otherwise false. More... | |
Date | lastModified |
The last modification time of the file. More... | |
Date | lastRead |
The last time the file was read. More... | |
String | name |
The name of the file including the extension. More... | |
String | path |
The path of the file. More... | |
Boolean | readable |
True if the file is readable; otherwise false. More... | |
Number | size |
The size of the file, in bytes. More... | |
String | symLink |
The expansion of the symlink if the file is a symlink; otherwise empty. More... | |
Boolean | writable |
True if the file is writable; otherwise false. More... | |
The bps.File class provides functionality for reading and writing binary and text files.
A File can be instantiated as an object, giving the scripter complete flexibility when reading and writing files. In addition, the File class provides a set of static convenience functions for reading and writing files in one go. Text reading and writing uses the UTF-8 encoding by default, but alternate encoding may be selected as Local8Bit, Latin1 and IBM 850 for the non-static read and write methods.
This enum is used with open() to describe the mode in which a device is opened.
bps::File::File | ( | String | aFileName | ) |
Creates a file object with the with fileName.
aFileName | The file name to use. |
Error | thrown if file name is missing or is not a String. |
|
static |
Check if file exists.
aFileName | The name of the file. |
|
static |
Check if aFileName is a directory.
aFileName | The name of the file. |
|
static |
Check if aFileName is a file.
aFileName | The name of the file. |
void bps::File::open | ( | AccessMode | aAccessMode | ) |
Opens the file in the specified access mode if possible; otherwise throws an exception.
aAccessMode | The file accessmode flag(s). |
Error | thrown if open fails. |
String bps::File::read | ( | ) |
Returns the entire content of the file as a string if it can be read; otherwise throws an exception.
Error | thrown if read fails. |
Reads and returns the contents of the file fileName if possible; otherwise throws an exception.
aFileName | The name of the file. |
Error | thrown if read fails. |
Number bps::File::readByte | ( | ) |
Reads one byte from the file if possible; otherwise throws an exception.
Error | thrown if read fails. |
String bps::File::readLine | ( | ) |
Reads one line from file if possible; otherwise throws an exception.
Retains any trailing whitespace.
Error | thrown if read fails. |
Array bps::File::readLines | ( | ) |
Read the contents of the file as an array of Strings, one for each line.
Linebreaks are strippped from the strings. If the file could not be read, an exception is thrown.
void bps::File::remove | ( | ) |
Deletes the file if possible; otherwise throws an exception.
Error | thrown if remove fails. |
|
static |
Deletes the file if possible; otherwise throws an exception.
aFileName | The name of the file. |
Error | thrown if remove fails. |
String bps::File::toString | ( | ) | const |
void bps::File::write | ( | String | aData | ) |
Writes data to the file if possible; otherwise throws an exception.
aData | The data to write. |
Writes the string content to the file fileName if possible (completely replacing the original contents if the file already exists); otherwise throws an exception.
aFileName | The name of the file. |
aContent | The content of the file. |
Error | thrown if write fails. |
void bps::File::writeByte | ( | Number | aByte | ) |
Writes a byte to the file is possible; otherwise throws an exception.
aByte | The numeric code of the byte to write. |
void bps::File::writeLine | ( | String | aData | ) |
Writes the line data to the file and adds a linebreak.
If the file could not be written error is returned.
aData | The data to write in a line. |
|
read |
The name of the file, excluding its path and extension.
|
read |
The creation time of the file.
|
readwrite |
Default encoding used for static text read/write methods, and initial encoding for files opened subsequently in script context.
The initial default encoding at application start is UTF-8.
Constant | Encoding |
---|---|
bps.File.Utf8 | UTF-8 |
bps.File.Latin1 | Latin 1 |
bps.File.Local8Bit | Local 8 Bit |
bps.File.IBM850 | IBM 850 |
|
readwrite |
Encoding for the non-static text read and write methods.
The encoding of files opened in script context is initialized to the value of defaultEncoding at opening time. The encoding of files opened in C++ context is bps.File.Default.
Constant | Encoding |
---|---|
bps.File.Default | Use defaultEncoding. |
bps.File.Utf8 | UTF-8 |
bps.File.Latin1 | Latin 1 |
bps.File.Local8Bit | Local 8 Bit |
bps.File.IBM850 | IBM 850 |
|
read |
True if reading has reached the end of the file; otherwise false.
|
read |
True if the file is executable; otherwise false.
|
read |
True if the file exists; otherwise false.
|
read |
The file name's extension.
|
read |
The fullName of the file, including path, name, and extension.
|
read |
True if the file is hidden; otherwise false.
|
read |
The last modification time of the file.
|
read |
The last time the file was read.
|
read |
The name of the file including the extension.
|
read |
The path of the file.
|
read |
True if the file is readable; otherwise false.
|
read |
The size of the file, in bytes.
|
read |
The expansion of the symlink if the file is a symlink; otherwise empty.
|
read |
True if the file is writable; otherwise false.