FileHandle
The FileHandle
class has functions that allow you to sequentially read from or append contents to an opened file
object. A file handle can access any part of a document.
File handle objects are created with the file.open()
function.
To read or write a whole document at once, you might consider using the file.getText() and file.setText() functions.
Thanks to the Qodly object refcounting, a file handle is automatically deleted when it is no longer referenced and thus, the requested file
object is automatically closed. Consequently, with file handles you don't need to worry about closing documents.
Example
var f : 4D.File
var fhandle : 4D.FileHandle
f = file("/PACKAGE/example.txt")
//Writing line by line from the start
fhandle = f.open("write")
text = "Hello World"
for (line, 1, 4)
fhandle.writeLine(text+string(line))
end
//Writing line by line from the end
fhandle = f.open("append")
text = "Hello New World!"
for (line, 1, 4)
fhandle.writeLine(text+string(line))
end
//Reading using a stop character and an object parameter
o = newObject()
o.mode = "read"
o.charset = "UTF-8"
o.breakModeRead = Document with CRLF
stopChar = "!"
fhandle = f.open(o)
text = fhandle.readText(stopChar)
//Reading line by line
lines = newCollection()
fhandle = f.open("read")
while (Not(fhandle.eof))
lines.push(fhandle.readLine())
end
Functions and properties
File handle objects cannot be shared.
.breakModeRead : string the processing mode for line breaks used when reading the file |
.breakModeWrite : string the processing mode for line breaks used when writing to the file |
.charset : string the charset used when reading from or writing to the file |
.eof : boolean True is the offset has reached the end of the file, and False otherwise |
.file : 4D.File the 4D.File object on which the handle has been created |
.getSize() : number returns the current size of the document, expressed in bytes |
.mode : string the mode in which the file handle was created: "read", "write", or "append" |
.offset : number the current offset of the data stream (position inside the document) |
[.readBlob( bytes : number ) : 4D.Blob ](#readblob) returns a blob a bytes size from the file, starting from the current position |
.readLine() : string returns a line of text from the current position until an end-of-line delimiter is encountered or the end of the document is reached |
.readText( { stopChar : string } ) : string returns text from the file, starting from the current position until the first stopChar string is encountered (if passed) or the end of file is reached |
.setSize( size : number ) sets a new size in bytes for the document |
.writeBlob( blob : 4D.Blob ) writes blob into the file, starting from the current position |
.writeLine( lineOfText : string ) writes lineOfText content at the current position and inserts an end-of-line delimiter |
.writeText( textToWrite : string ) writes textToWrite content at the current position and does not insert a final end-of-line delimiter |
.breakModeRead
.breakModeRead : string
Description
The .breakModeRead
property returns the processing mode for line breaks used when reading the file.
The .breakModeRead
property can be defined at the handle creation with the file.open()
function (see the .open()
function for more information). Default is "native".
The
.breakModeRead
property always contains a text value, even if the.open()
option was set using a number (constant).
This property is read-only.
.breakModeWrite
.breakModeWrite : string
Description
The .breakModeWrite
property returns the processing mode for line breaks used when writing to the file.
The .breakModeWrite
property can be defined at the handle creation with the file.open()
function (see the .open()
function for more information). Default is "native".
The
.breakModeWrite
property always contains a text value, even if the.open()
option was set using a number (constant).
This property is read-only.
.charset
.charset : string
Description
The .charset
property returns the charset used when reading from or writing to the file.
The charset can be defined at the handle creation with the file.open()
function. Default is "UTF-8".
This property is read-only.
.eof
.eof : boolean
Description
The .eof
property returns True is the offset
has reached the end of the file, and False otherwise.
This property is read-only.
.file
.file : 4D.File
Description
The .file
property returns the 4D.File object on which the handle has been created.
This property is read-only.
.getSize()
.getSize() : number
Parameter | Type | Description | |
---|---|---|---|
Result | number | ← | Size of the document in bytes |
Description
The .getSize()
function returns the current size of the document, expressed in bytes.
This function returns the same value as the (.size) property of the
file
class.
See also
.mode
.mode : string
Description
The .mode
property returns the mode in which the file handle was created: "read", "write", or "append".
The mode can be defined at the handle creation with the file.open()
function. Default is "read".
This property is read-only.
.offset
.offset : number
Description
The .offset
property returns the current offset of the data stream (position inside the document). The offset value is automatically updated after read and write operations.
Setting the .offset
will change its current value.
- If the passed value is negative, the
.offset
is set to the start of the file (zero). - If the passed value is higher than the size of the file, the
.offset
is set to the end of the file (size of file).
This property is read/write.
When a file handle is created, the .offset
value is a number of bytes. However, the unit of offset measurement differs according to the reading function: with readBlob()
, .offset
is a number of bytes, whereas with readText()
/readLine()
it is a number of characters. Depending on the file's character set, a character corresponds to one or more bytes. So, if you start reading with readBlob()
and then call readText()
, text reading will start at an inconsistent position. It is therefore essential to set the .offset
property yourself if you switch from reading/writing blob to reading/writing text in the same filehandle. For example:
// Open a european text file using utf-16 encoding (two bytes per character)
// We want to read the first 10 characters as bytes, then the remaining as text.
var vFileHandle : 4D.File
var vBlob : 4D.Blob
var vString : string
vFileHandle = file("/SOURCES/Shared/sample_utf_16.txt").open()
// read the 20 first bytes (i.e. 10 characters)
vBlob = vFileHandle.readBlob(20) // vFileHandle.offset=20
// then read all text skipping the first 10 characters we just read in previous blob
// because we are now reading text instead of bytes, the meaning of 'offset' is not the same.
// We need to translate it from bytes to characters.
vFileHandle.offset = 10 // ask to skip 10 utf-16 characters (20 bytes)
vString = vFileHandle.readText()
.readBlob()
.readBlob( bytes : number ) : 4D.Blob
Parameter | Type | Description | |
---|---|---|---|
bytes | number | → | Number of bytes to be read |
Result | 4D.Blob | ← | Bytes read from the file |
Description
The .readBlob()
function returns a blob a bytes size from the file, starting from the current position .
When this function is executed, the current position (.offset) is updated after the last byte read.
See also
.readLine()
.readLine() : string
Parameter | Type | Description | |
---|---|---|---|
Result | string | ← | Line of text |
Description
The .readLine()
function returns a line of text from the current position until an end-of-line delimiter is encountered or the end of the document is reached.
When this function is executed, the current position (.offset) is updated.
This function assumes that the .offset
property is a number of characters, not a number of bytes. For more information, see the .offset description.
When this function is executed for the first time on a file handle, the whole document contents is loaded in a buffer.
See also
.readText()
.readText( { stopChar : string } ) : string
Parameter | Type | Description | |
---|---|---|---|
stopChar | string | → | Character(s) at which to stop reading |
Result | string | ← | string from the file |
Description
The .readText()
function returns text from the file, starting from the current position until the first stopChar string is encountered (if passed) or the end of file is reached.
The stopChar character string is not included in the returned text. If you omit the stopChar parameter, the whole document text is returned.
When this function is executed, the (.offset) is placed just after the stopChar string.
This function assumes that the .offset
property is a number of characters, not a number of bytes. For more information, see the .offset description.
If the stopChar parameter is passed and not found, .readText()
returns an empty string and the .offset is left untouched.
When this function is executed for the first time on a file handle, the whole document contents is loaded in a buffer.
See also
.setSize()
.setSize( size : number )
Parameter | Type | Description | |
---|---|---|---|
size | number | → | New size of the document in bytes |
Description
The .setSize()
function sets a new size in bytes for the document.
If the size value is less than the current document size, the document content is truncated from the beginning to get the new size .
See also
.writeBlob()
.writeBlob( blob : 4D.Blob )
Parameter | Type | Description | |
---|---|---|---|
blob | 4D.Blob | → | Blob to write in the file |
Description
The .writeBlob()
function writes blob into the file, starting from the current position .
When this function is executed, the current position (.offset) is updated after the last byte written.
See also
.writeLine()
.writeLine( lineOfText : string )
Parameter | Type | Description | |
---|---|---|---|
lineOfText | string | → | string to write |
Description
The .writeLine()
function writes lineOfText content at the current position and inserts an end-of-line delimiter (unlike the .writeText() function). By default, a native end-of-line delimiter is used, but you can define another delimiter when opening the file handle by setting the .breakModeWrite
property.
When this function is executed, the current position (.offset) is updated after the end-of-line delimiter.
See also
.breakModeWrite, .readLine(), .writeText()
.writeText()
.writeText( textToWrite : string )
Parameter | Type | Description | |
---|---|---|---|
textToWrite | string | → | string to write |
Description
The .writeText()
function writes textToWrite content at the current position and does not insert a final end-of-line delimiter (unlike the .writeLine() function). This function replaces all original end-of-line delimiters. By default, the native delimiter is used, but you can define another delimiter when opening the file handle by setting the .breakModeWrite
property.
When this function is executed, the current position (.offset) is updated after the next end-of-line delimiter.