AX – D365FO – Write and Read Txt file with TextIo class

The following X++ job code sample creates a file and writes to it. Next the code reads from the file, and prints every record to the Infolog.



static void Job_File_IO_TextIo_Write_Read(Args _args)
    {
        TextIo txIoRead, txIoWrite;
        FileIOPermission fioPermission;
        container containFromRead;
        int xx, iConLength;
        str sTempPath, sFileName = "Test_File_IO.txt", sOneRecord;
        

        // Get the temporary path.
        sTempPath = WINAPI::getTempPath();
        info("File is at: " + sTempPath + sFileName);
    
        // Assert permission.
        fioPermission = new FileIOPermission
            (sTempPath + sFileName ,"RW");
        fioPermission.assert();
     
        // If the test file already exists, delete it.
        if (WINAPI::fileExists(sFileName))
        {
            WINAPI::deleteFile(sTempPath + sFileName);
        }
        
        // Open a test file for writing.
        // "W" mode overwrites existing content, or creates the file.
        txIoWrite = new TextIo( sTempPath + sFileName ,"W");
    
        // Write records to the file.
        txIoWrite.write("Hello        World.");
        txIoWrite.write("The sky is blue.");
        txIoWrite.write("");
        txIoWrite.write("// EOFile");
    
        // Close the test file.
        txIoWrite = null;
    
        // Open the same file for reading.
        txIoRead = new TextIo(sTempPath + sFileName ,"R");
        // Read the entire file into a container.
        containFromRead = txIoRead.read();
    
        // Loop through the container of file records.
        while (containFromRead)
        {
            sOneRecord = "";
            iConLength = conLen(containFromRead);
            // Loop through the token in the current record.
            for (xx=1; xx <= iConLength; xx++)
            {
                if (xx > 1) sOneRecord += " ";
                sOneRecord += conPeek(containFromRead ,xx);
            }
            info(sOneRecord);
    
            // Read the next record from the container.
            containFromRead = txIoRead.read();
        }
    
        // Close the test file.
        txIoRead = null;
        // Delete the test file.
        WINAPI::deleteFile(sTempPath + sFileName);
    
        // revertAssert is not really necessary here,
        // because the job (or method) is ending.
        CodeAccessPermission::revertAssert();
    }

This is the output result

Message (14:12:47)
File is at: C:\DOCUME~1\myalias\LOCALS~1\Temp\Test_File_IO.txt
Hello World.
The sky is blue.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s