File data type: Text file handlingOfficial Content

We can use File data type to handle text types. Besides the copying, deleting, and handling files operations, it allows us to manage the content of text files (read or write the text in one step or do it by line).

It is quite useful for scenarios where the file content must be dumped directly into a memory variable, or in the case of writing a log file with the content of a string variable.

Below are the methods available for File data type meant for reading or writing text files.

Methods for reading or writing text files

The following methods are for reading or writing text files. The operation is done in one step: with the open file, contents are dumped into memory, and then the file is closed. This is very useful for small files. For the case of files of many kbytes, looping and reading each line is more recommended.
 

Method Description
ReadAllText([encoding]):String Opens a text file, reads all lines in the file, and closes the file, using the optionally specified encoding (UTF-8 when not specified).
ReadAllLines([encoding]):StringCollection Opens a text file, reads all lines in the file, and closes the file. Each item in the collection is a line from the file, using the optionally specified encoding (UTF-8 when not specified). The value returned is a Character Collection.
WriteAllText(String[, encoding]) Opens the file, writes the string specific to the file, and closes the file. If the file has content, it is overwritten, using the optionally specified encoding (UTF-8 when not specified).
WriteAllLines(StringCollection[, encoding]) Opens the file, writes the string collection specific for the file, each string followed by a line terminator, and closes the file. If the file has content, it is overwritten, using the optionally specified encoding (UTF-8 when not specified).
AppendAllText(String[, encoding]) Opens the file, appends the specified string to the file, and closes the file, using the optionally specified encoding (UTF-8 when not specified).
AppendAllLines(StringCollection[, encoding]) Opens the file, appends the specified string collection to the file, each string followed by a line terminator, and closes the file, using the optionally specified encoding (UTF-8 when not specified).

Example I

In this example we search for files with html extension and load their contents in the database. To scan the files we use a variable &File (based on File data type), and a variable &Directory (based on Directory data type), and then use the ReadAllText method to dump the content of the file into the database.

For &File in &Directory.GetFiles("*.html")
   New
      ContentHtml = &File.ReadAllText()
   EndNew
​EndFor

Example II

In this example we write a log file with data from a Business Component.

&LogLines = new()  //&LogLines is a string collection.
&LogLines.Add(Format("[%1] Error updating Customer", Now()))
&LogLines.Add(&CustomerBC.ToJson())
&LogFile.AppendAllLines(&LogLines) //&LogFile is File Data Type.

Methods for reading or writing lines in the text file

The following methods are applied for reading or writing a file by lines. 

Method  Description
ReadLine():String Reads a line of characters from the file. The string returned does not contain the terminating carriage return or line feed. The file must be opened before using this method (using OpenRead or Open method).
WriteLine(String) Writes the specified string followed by a line terminator. The file must be opened before using this method (using OpenWrite or Open method).


In this case the file must be opened prior to the operation. Following is a detail of the methods to be used for opening the file.

Method Description
Open([encoding]) Opens a file for read/write access, using the optionally specified encoding (UTF-8 when not specified). If the file exists, the content to write is appended to the file. Otherwise, a new file is created.
OpenWrite([encoding]) Opens a file for write access in order to append content, or creates a new file when one does not exist, using the optionally specified encoding (UTF-8 when not specified).
OpenRead([encoding]) Opens a file for read access, using the optionally specified encoding (UTF-8 when not specified).

Methods for creating and closing a text file

Method Description
Create()

Creates a text file in the path specified in the Source property. The file is not opened.
If the file already exists, it sets ErrCode = 1 and the file is not created nor truncated.

Close()  Closes the file.


Properties

Name Description
EOF True if the end of the file has been reached. Before using this property, the file must be opened for reading.

Example III

Processing a file by lines.

&File.Source = "C:\Files\Data.txt" //&File is File Data Type.
&File.OpenRead()
do while not &File.EOF
    ProcessLine(&File.ReadLine())
enddo
&File.Close()

FromJsonFile and FromXMLFile Methods 

The following methods allow to return and load a SDT or an XML variable from the contents of a file into this format (json or xml).

Method Description



FromJsonFile(File)

Opens a JSON file, parses its content, loads the SDT, and closes the file.
FromXMLFile(File) Opens a XML file, parses its content, loads the SDT, and closes the file.


Example IV

In the following example the file content is in json format, and it is compatible with the structure of CustomerSDT SDT.

&File.Source = "C:\Files\data.json" //&File is File Data Type
&CustomerSDT.FromJsonFile(&File) //&CustomerSDT is a SDT variable

Availability

Genexus Tilo RC (X Ev3)

Scope

Objects Procedure object, Transaction object, Web Panel object
Languages .NET, Java, Ruby
Applies to Web, Smart Devices

These methods run in Smart Devices Offline Applications.

See also

File data type
Directory data type