TextFile Service

class TextFile

Allows to read from and write into text files.

Enumerations:OpenMode
Methods:TextFile::TextFile(), atEndOfFile(), close(), readAll(), readLine(), truncate(), write(), writeLine()

Detailed Description

The TextFile service provides convenient access to text files in Javascript context. For instance, within Testcase::run():

function run() {
    // ...
    var file = new TextFile(test.workingDirectory + "/hello.txt",
            TextFile.WriteOnly, "UTF-8");
    file.write("Hello World");
    file.close();
    // ...
}

TextFile is to be used in Javascript context only and cannot be instantiated as a QML component.

Enumerations

enum OpenMode

Specified an access qualifier for the file. Options may be OR’ed togther:

new TextFile(path, TextFile.ReadWrite | TextFile.Append, "UTF-8");
enumerator Append

The file is opened for appending text. This is option may be combined with WriteOnly.

enumerator ReadOnly

Opens the file for read access only. Several instances may open the file in read-only mode at the same time.

enumerator ReadWrite

Opens the file for read and write access.

enumerator WriteOnly

Opens the file for write access only.

Methods

TextFile(string filePath, OpenMode mode, string codec)

Constructs a TextFile object and opens the file filePath with mode access permission and using a text codec specified by codec.

The filePath must be an absolute path. Options for codec are the same as for QTextCodec, for instance “UTF-8”, “UTF-16”, “ISO 8859-1” and others.

The default value for mode is OpenMode::ReadWrite. The default value for codec is “UTF-8”.

bool atEndOfFile()

Returns true if no more data can be read from the file, false otherwise.

void close()

Closes the file. It is recommended to always call this function as soon as you are finished with the file, in order to keep the number of in-flight file descriptors as low as possible.

string readAll()

Reads all data from the file and returns it.

string readLine()

Reads one line of text from the file and returns it. The returned string does not contain the newline characters.

void truncate()

Truncates the file. Sets the file size to zero and removes all content.

void write(string data)

Writes data into the file at the current position.

void writeLine(string data)

Writes data into the file at the current position and appends the newline character(s).