The FileReader Object

A FileReader object is used to read a text file on the script server. For non text files use the BinaryFile object.

FileReader objects are of class FileReader.

FileReader Object Properties

Property

Description

error

An error message If an error has occurred. If no error has occurred then it will be null. The error messages are explained below:

File not found - The file specified by the name does not exist.

No read access for file - The file specified by the name can not be read. This could be because the file does not exist or because we do not have permission to read it.

Encoding not found - The encoding specified in the constructor is not a recognized encoding. This means that you will not be able to read this file.

While accessing the file the operating system can generate various other error messages.

prototype

A reference to the Object Prototype. This property does not enumerate and can not be deleted but can be changed.

FileReader Object Functions

Function

Description

close()

Closes the file and frees resources. Returns true if there was no error and false if an error occurred.

exists()

Checks to see if the file exists and returns true if it does exist and false if it does not. If an error occurs then this function returns false and the error property will be set.

read(length)

Reads the number of characters specified by the length argument from the file and returns a string. If there are not enough characters remaining in the file it returns all remaining characters.

A read will return null if at end of file. If an error occurs then the error property will be set.

readln()

Reads until a new line character is found and returns the line as a string not including the new line character.

A read will return null if at end of file. If an error occurs then the error property will be set.


The FileReader Constructor

Using the new Operator with the FileReader Constructor

The FileReader constructor is used to create new FileReader objects.

var myFileReader = new FileReader(name[, encoding]);

A new FileReader object for the file identified by the name is created. The name uses the script file reference to identify the file. The file can be anywhere on the host filing system. If the name argument is not present then a runtime error will be generated and the script page execution halted. The encoding argument identifies the encoding required to read the file. If this argument is not present then the default script encoding (defined in the Active Web console) is used.

Shown below is the FileReader object prototype chain.

Calling the FileReader Constructor as a Function

When the constructor is run as a function without the new operator it has exactly the same behaviour as using the new operator.

FileReader Constructor Properties

Constructor properties are read only (they can not be changed or deleted) and it is not possible to add new properties to the Constructor.

Property

Description

prototype

A reference to the Object Prototype. This property does not enumerate and can not be changed or deleted.

FileReader Constructor Functions

The FileReader constructor has no predefined functions.

Comment on this topic

Topic ID: 150052