AX provides several ways of writing files. Depending upon scenario, you can use one. Apart from AX native filing, you can also use .NET interop to call .NET classes for reading/writing files. We will be showing few classes which are provided by AX for writing files.
AsciiIO
AsciiIO class is another class used to write file.
This class only uses ASCII characters to write file. This class is inherited
from CommaIo class, so you can also make use of column delimiter and record
delimiter.
I had a situation when I was working for a client
to build an export file mechanism to be imported at a bank. I used TextIO class
to export the data to file but that file couldn’t be imported at Bank because
of some error. The file format and the data when seen in text editor, appeared
perfect but we were unable to identify the correct problem. Later on it was
noted that the character encoding might not be as per expectation. Then the use
of TextIO class was discarded (for that particular scenario only) and used
AsciiIO class to export the file. This fixed the problem.
Read Text File Using AsciiIO
static void ReadTextFileAscii(Args _args) { #File AsciiIo readFile; str line; container fileRecord; InteropPermission interopPermission; str text,filepath; FileIOPermission permission; containerconfilter =['.txt','*.txt']; A_PurchaseOrder objA_PurchaseOrder; //FileIoPermission permission; filepath = WinAPI::getOpenFileName( 0, confilter, "", "Save As", "", "Untitled") ; interopPermission = newInteropPermission(InteropKind::ClrInterop); permission = newFileIOPermission(filepath,#io_read); interopPermission.assert(); readFile = newAsciiIo(filepath,'r'); //if (!readFile) //throw Exception::Error; //readFile.inRecordDelimiter(#delimiterCRLF); //readFile.inFieldDelimiter("|"); //if (!readFile || readFile.status() != IO_Status::Ok) //{ //throw error("File cannot be opened."); //} readFile.outRecordDelimiter(#delimiterCRLF); //readFile.inFieldDelimiter("|"); fileRecord = readFile.read(); while (fileRecord) { line = con2str(fileRecord); info(line); fileRecord = readFile.read(); } }
Write Text Using AsciiIO
static void WriteTextAsciiIO(Args _args) { #File AsciiIo WriteLine; container fileRecord; InteropPermission interopPermission; str text,filepath; FileIOPermission permission; containerconfilter =['.txt','*.txt']; A_PurchaseOrder objA_PurchaseOrder; //FileIoPermission permission; container line; filepath = WinAPI::getSaveFileName( 0, confilter, "", "Save As", "", "Untitled") ; interopPermission = newInteropPermission(InteropKind::ClrInterop); permission = newFileIOPermission(filepath,#io_write); interopPermission.assert(); WriteLine = newAsciiIo(filepath,'w'); WriteLine.outRecordDelimiter(#delimiterCRLF); WriteLine.outFieldDelimiter(' | ');// for semicolon seperator if (!WriteLine || WriteLine.status() != IO_Status::Ok) { throw error("File cannot be opened."); } whileselectobjA_PurchaseOrder { line = [objA_PurchaseOrder.Purchase_ID,objA_PurchaseOrder.Vender_Code, enum2str(objA_PurchaseOrder.Status),date2str(objA_PurchaseOrder.Purchase_Date,123, DateDay::Digits2,DateSeparator::Hyphen,DateMonth::Digits2, DateSeparator::Hyphen,DateYear::Digits4), objA_PurchaseOrder.Purchase_Amount]; WriteLine.write(line); } CodeAccessPermission::revertAssert(); }
Thanks for comments.....