BPS Script API  2.24.4
bps::File Class Reference

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...
 

Detailed Description

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.

// Reads an entire file in one go
var log = bps.File.read('file.log');
// Writes an entire file in one go
bps.File.write('copy_of_file.log', log);
// Read and write a file line by line
var infile = new bps.File('file.log');
var outfile = new bps.File('copy_of_file.log');
while (!infile.eof) {
var line = infile.readLine();
outfile.write( line );
}
infile.close();
outfile.close();
The bps.File class provides functionality for reading and writing binary and text files.
Definition: bps.File.js:35
void write(String aData)
Writes data to the file if possible; otherwise throws an exception.
void open(AccessMode aAccessMode)
Opens the file in the specified access mode if possible; otherwise throws an exception.
@ ReadOnly
Opens the file in read-only mode.
Definition: bps.File.js:151
@ WriteOnly
Opens the file in write-only mode.
Definition: bps.File.js:152
String read()
Returns the entire content of the file as a string if it can be read; otherwise throws an exception.
The bps extension is a namespace assembling general BPS properties and functions.
Definition: bps.AsyncIO.js:1
void log(...)
Print to log output.

Member Enumeration Documentation

◆ AccessMode

This enum is used with open() to describe the mode in which a device is opened.

Enumerator
ReadOnly 

Opens the file in read-only mode.

WriteOnly 

Opens the file in write-only mode.

ReadWrite 

The device is open for reading and writing.

Append 

The device is opened in append mode, so that all data is written to the end of the file.

Truncate 

If possible, the device is truncated before it is opened. All earlier contents of the device are lost.

Text 

When reading, the end-of-line terminators are translated to '\n'. When writing, the end-of-line terminators are translated to the local encoding, for example '\r\n' for Win32.

Unbuffered 

Any buffer in the device is bypassed.

Constructor & Destructor Documentation

◆ File()

bps::File::File ( String  aFileName)

Creates a file object with the with fileName.

Parameters
aFileNameThe file name to use.
Exceptions
Errorthrown if file name is missing or is not a String.

Member Function Documentation

◆ exists()

static Boolean bps::File::exists ( String  aFileName)
static

Check if file exists.

Parameters
aFileNameThe name of the file.
Returns
True if aFileName exists, false otherwise.

◆ isDir()

static Boolean bps::File::isDir ( String  aFileName)
static

Check if aFileName is a directory.

Parameters
aFileNameThe name of the file.
Returns
True if aFileName is a directory; otherwise returns false.

◆ isFile()

static Boolean bps::File::isFile ( String  aFileName)
static

Check if aFileName is a file.

Parameters
aFileNameThe name of the file.
Returns
True if aFileName is a file; otherwise returns false.

◆ open()

void bps::File::open ( AccessMode  aAccessMode)

Opens the file in the specified access mode if possible; otherwise throws an exception.

Parameters
aAccessModeThe file accessmode flag(s).
Exceptions
Errorthrown if open fails.

◆ read() [1/2]

String bps::File::read ( )

Returns the entire content of the file as a string if it can be read; otherwise throws an exception.

Returns
The file content.
Exceptions
Errorthrown if read fails.

◆ read() [2/2]

static String bps::File::read ( String  aFileName)
static

Reads and returns the contents of the file fileName if possible; otherwise throws an exception.

Parameters
aFileNameThe name of the file.
Returns
The content of the file.
Exceptions
Errorthrown if read fails.

◆ readByte()

Number bps::File::readByte ( )

Reads one byte from the file if possible; otherwise throws an exception.

Returns
The numeric code of the byte read.
Exceptions
Errorthrown if read fails.

◆ readLine()

String bps::File::readLine ( )

Reads one line from file if possible; otherwise throws an exception.

Retains any trailing whitespace.

Returns
The line read if not eof.
Exceptions
Errorthrown if read fails.

◆ readLines()

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.

Returns
Array of String with the file contents.

◆ remove() [1/2]

void bps::File::remove ( )

Deletes the file if possible; otherwise throws an exception.

Exceptions
Errorthrown if remove fails.

◆ remove() [2/2]

static void bps::File::remove ( String  aFileName)
static

Deletes the file if possible; otherwise throws an exception.

Parameters
aFileNameThe name of the file.
Exceptions
Errorthrown if remove fails.

◆ toString()

String bps::File::toString ( ) const
Returns
Returns "File(file name)"

◆ write() [1/2]

void bps::File::write ( String  aData)

Writes data to the file if possible; otherwise throws an exception.

Parameters
aDataThe data to write.

◆ write() [2/2]

static void bps::File::write ( String  aFileName,
String  aContent 
)
static

Writes the string content to the file fileName if possible (completely replacing the original contents if the file already exists); otherwise throws an exception.

Parameters
aFileNameThe name of the file.
aContentThe content of the file.
Exceptions
Errorthrown if write fails.

◆ writeByte()

void bps::File::writeByte ( Number  aByte)

Writes a byte to the file is possible; otherwise throws an exception.

Parameters
aByteThe numeric code of the byte to write.

◆ writeLine()

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.

Parameters
aDataThe data to write in a line.

Property Documentation

◆ baseName

String bps::File::baseName
read

The name of the file, excluding its path and extension.

 

◆ created

Date bps::File::created
read

The creation time of the file.

 

◆ defaultEncoding

Number bps::File::defaultEncoding
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.

ConstantEncoding
bps.File.Utf8UTF-8
bps.File.Latin1Latin 1
bps.File.Local8BitLocal 8 Bit
bps.File.IBM850IBM 850
Note
Changing the default encoding will not change the encoding of files previously opened in script context. It will however effect existing files created in C++ context, when the encoding property was not yet changed from within a script.

◆ encoding

Number bps::File::encoding
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.

ConstantEncoding
bps.File.DefaultUse defaultEncoding.
bps.File.Utf8UTF-8
bps.File.Latin1Latin 1
bps.File.Local8BitLocal 8 Bit
bps.File.IBM850IBM 850

◆ eof

Boolean bps::File::eof
read

True if reading has reached the end of the file; otherwise false.

 

◆ executable

Boolean bps::File::executable
read

True if the file is executable; otherwise false.

 

◆ exists

Boolean bps::File::exists
read

True if the file exists; otherwise false.

 

◆ extension

String bps::File::extension
read

The file name's extension.

 

◆ fullName

String bps::File::fullName
read

The fullName of the file, including path, name, and extension.

 

◆ hidden

Boolean bps::File::hidden
read

True if the file is hidden; otherwise false.

 

◆ lastModified

Date bps::File::lastModified
read

The last modification time of the file.

 

◆ lastRead

Date bps::File::lastRead
read

The last time the file was read.

 

◆ name

String bps::File::name
read

The name of the file including the extension.

 

◆ path

String bps::File::path
read

The path of the file.

 

◆ readable

Boolean bps::File::readable
read

True if the file is readable; otherwise false.

 

◆ size

Number bps::File::size
read

The size of the file, in bytes.

 

◆ symLink

String bps::File::symLink
read

The expansion of the symlink if the file is a symlink; otherwise empty.

 

◆ writable

Boolean bps::File::writable
read

True if the file is writable; otherwise false.

 


The documentation for this class was generated from the following file: