Manually writing to and reading from files
Apart from the built-in object types for creating text files and XML files, you can also create them manually. This recipe will show you how to write your own code to do this.
How to do it...
Create a new codeunit from Object Designer.
Add the following global variables:
Name
DataType
StreamOut
OutStream
FileOut
File
StreamIn
InStream
FileIn
File
Add the following code to the
OnRun
trigger:IF NOT FileOut.CREATE('C:\NAVFile.txt') THEN IF NOT FileOut.OPEN('C:\NAVFile.txt') THEN ERROR('Unable to write to file!'); FileOut.CREATEOUTSTREAM(StreamOut); StreamOut.WRITETEXT('Line 1'); StreamOut.WRITETEXT(); StreamOut.WRITETEXT('Line 2'); StreamOut.WRITETEXT(); FileOut.CLOSE; IF NOT FileIn.OPEN('C:\NAVFile.txt') THEN ERROR('Unable to read file!'); FileIn.CREATEINSTREAM(StreamIn); WHILE NOT StreamIn.EOS DO BEGIN StreamIn.READTEXT(TextLine); MESSAGE('%1', TextLine); END; FileIn.CLOSE;
Save and close the codeunit.
How it works...
First we try to create a...