TextFile Service
The TextFile
service allows you to read from and write into text files.
Making the Service Available
In order to gain access to text file operations, you need to import the service using the following statement at the top of your project file:
import qbs.TextFile
Available operations
Constructor
TextFile(filePath, openMode)
Opens the file at filePath
in the given mode and returns the object representing the file. Possible values for openMode
are TextFile.ReadOnly
(which is the default), TextFile.WriteOnly
and TextFile.ReadWrite
. Note that the mode influences which of the operations listed below can actually be used on the file.
atEof
atEof()
Returns true if no more data can be read from the file, false otherwise.
close
close()
Closes the file. We 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.
readAll
readAll()
Reads all data from the file and returns it.
readLine
readLine()
Reads one line of text from the file and returns it. The returned string does not contain the newline characters.
setCodec
setCodec(codec)
Sets the text codec to codec
. The supported codecs are the same as for QTextCodec
, for example: "UTF-8", "UTF-16", and "ISO 8859-1".
truncate
truncate()
Truncates the file, that is, gives it the size of zero, removing all content.
write
write(data)
Writes data
into the file at the current position.
writeLine
writeLine(data)
Writes data
into the file at the current position and appends the newline character(s).