Files, Folders… and now File Handles!

The File and Folder commands, which appeared in 4D v17 R5, enable you to manage folders and files on disk in a modern and efficient way.

When it comes to writing and reading, the setText and getText functions are at your disposal to read or write the entire content. But what to do if you want to read or add one or more lines to an existing document? This can be done more efficiently than ever using the new “file handles” objects in 4D v19 R7.

Keep reading for details!

HDI Use file handles

What are File handles?

File handles are objects created based on “File” objects and have functions to access any part of documents and, from there, to read or write their content sequentially.

Even if, in the general case, we read from the beginning to the end, it is possible to define precisely the position of the “reading head.”

In the same way, when you want to open a document to write new information, it is generally at the end of the document you want to write. You can position the “writing head” wherever you want.

But it’s not all!

The file handle’s primary and most significant feature is that it will avoid opening and closing documents, which is a big time-consuming operation. And the best thing is closing files by programming is no longer mandatory. As soon as the file handled is not referenced anywhere in your code, the file will be closed automatically! That’s why, as we’ll see later, no .close() function is needed!

Let’s dive into a quick sample.

The goal is to add lines to a log file. Let’s create a class called Logger and a function called writeLine and see how to call it !

Class constructor($logName : Text)
	var $file : 4D.File
	$file.parent.create()  // check that parent folder exists; Otherwise it will be created.
Function writeLine($logText : Text)

Once this class is created, let’s call it !

var $logger : cs.Logger
var $logName : Text

$logName:=Replace string(Timestamp; ":"; "-")+".txt"

For ($i; 1; 10)
End for 

At this point, we could close the file by setting the $logger object to NULL, but it’s not mandatory ! At the end of this code, the $logger (and its attribute .fh) will be NULL so the file will be closed!

Attributes and functions

New file Function:

The first function, open(), is a new function of the File allowing the creation of the handle. The handle can be created in read, write, or append modes.


You can also use an object as a parameter if you need to specify the charset and the read or write delimiters.

$o:=New object()
$o.breakModeRead:=Document with CRLF
$o.breakModeWrite:=Document with CRLF

Once the handle is created, more attributes and functions will be available:

Two more Handle useful Attributes

  • .offset is the current offset of the data stream (position inside the document)
    • This attribute is writable. Use it only if you need to set the read or write position at a specific place. Just remember its values changes automatically after a read or write operation.
  • .eof is false as long as the end of the document is not reached.


The .eof attribute allows you to browse a file in an exquisite way

While (Not($fileHandle.eof))  // eof = end of file
End while 

handle Functions

The handle class comes with many functions that allow to:

  • Read and write text
  • Read and write lines (with automatic management of line breaks)
  • Get and set the size of a document
  • Read and write Blobs! 

Where to go from here?

Find more details on the documentation center. And feel free to reach out to us on the 4D forums; we will be happy to help.

Roland Lannuzel
• Product Owner & 4D Expert •After studying electronics, Roland went into industrial IT as a developer and consultant, building solutions for customers with a variety of databases and technologies. In the late 80’s he fell in love with 4D and has used it in writing business applications that include accounting, billing and email systems.Eventually joining the company in 1997, Roland’s valuable contributions include designing specifications, testing tools, demos as well as training and speaking to the 4D community at many conferences. He continues to actively shape the future of 4D by defining new features and database development tools.