InputFileStream functions

class InputFileStream(filename, format)

To use any of the functions below you first have to create an InputFileStream object.

The constructor creates and opens the specified input file stream. Streams can be opened either in BINARY or UTF8 format.

Parameters:
  • filename (String) – Specifies the name of the file to be opened. Can be specified as an absolute path or relative to the project directory.

  • format (String) – Specifies the format of the file to be opened: "utf8" or "binary"

See InputFileStream object properties below for how to check if the file could be opened.

Example:

var ifs = new InputFileStream("TestUtf8.txt", "utf8");
InputFileStream.open()

Opens the input file.

InputFileStream.close()

Closes the input file.

InputFileStream.read(size)

Read the given number of characters from the opened stream into a string. Reading a BINARY stream will convert each byte into a 16-bit CharCode of JavaScript string. Only plane 0, BMP is supported. In this case, elements in JavaScript can be accessed by .charCodeAt(index). Reading UTF8 streams assumes Utf-8 encoded 16-bit long unicode codes. Only plane 0, BMP is supported.

Parameters:
  • size (Integer) – Specifies the maximum number of characters to read. 0 means the whole content.

Returns:

The characters read converted into a JavaScript string. If no data can be read, the length of the JavaScript string is 0.

Example, reading and processing blocks of data from a stream:

for(;;)
{
    var data = ifs.read(1024);
    if (data.length > 0)
        process(data);
    if (ifs.eof)
        break; // reached end of file
}
InputFileStream.readLine()

Read the next line from the opened stream into a string. Reading a BINARY stream will convert each byte into a 16-bit CharCode of JavaScript string. Only plane 0, BMP is supported. In this case, elements in JavaScript can be accessed by .charCodeAt(index). Reading UTF8 streams assumes Utf-8 encoded 16-bit long unicode codes. Only plane 0, BMP is supported. The CR/LF is not included in the string. The default is to treat <CR/LF> as a line separator. If only CR or LF is given, treat CR or LF as a line separator. If the string is empty, either an empty line read or eof reached. This can be interrogated by the corresponding attribute.

Returns:

The line read converted into a JavaScript string.

Example:

var line = ifs.readLine();

InputFileStream object properties

name

Current file name

format

Current file format:

  • "utf8"

  • "binary"

mode

Not used

opened

File status:

  • true – The file is opened

  • false – The file is closed

good

File status:

  • true – The stream is intact

  • false – No more operations available

bad

File status:

  • true – No more operations available

  • false – The stream is intact

eof

File status:

  • true – End of file reached

  • false – Not at the end of file

path

Absolute path and file name