Description:-
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.
TextIO
This is the most commonly used class for text file
writing in AX. TextIO writes file in Unicode characters. File name (along with
path) and opening mode needs to be specified in the constructor.
Write in Text using TextIO
static void ExportInText(Args _args) { TextIO textIO; container line,line1; str filepath; Dialog dialog; DialogField dialogFileName; FileIoPermission permission; container confilter =['.txt','*.txt']; A_PurchaseOrder objA_PurchaseOrder; A_POIDSUB objA_POIDSUB; filepath = WinAPI::getSaveFileName( 0, confilter, "", "Save As", "", "Untitled") #File ; try { permission = newFileIoPermission(filepath, #io_write); permission.assert(); textIO = new TextIO(filepath, #io_write); textIO.outRecordDelimiter(#delimiterCRLF); textIO.outFieldDelimiter(' | ');// for semicolon seperator if (!textIO || textIO.status() != IO_Status::Ok) { throw error("File cannot be opened."); } whileselectobjA_PurchaseOrder { // Empty the container line = connull(); // Set the data into the container line = conins(line, 1, objA_PurchaseOrder.Purchase_ID); line = conins(line, 2, objA_PurchaseOrder.Vender_Code); line = conins(line, 3, enum2str(objA_PurchaseOrder.Status)); line = conins(line, 4, date2str(objA_PurchaseOrder.Purchase_Date,123,DateDay::Digits2,DateSeparator::Hyphen,DateMonth::Digits2 ,DateSeparator::Hyphen,DateYear::Digits4)); line = conins(line, 5, objA_PurchaseOrder.Purchase_Amount); // Write the container to the file textIO.writeExp(line); } } catch(Exception::Error) { error("You do not have access to write the file to the selected folder"); } // Revert the access privileges CodeAccessPermission::revertAssert(); }
Read in Text Using TextIO
static void ReadTextFile(Args _args) { TextIo file; str filepath; container confilter =['.txt','*.txt']; container record; FileIoPermission permission; str20 _PID,_VID,_IID,_IName; CSVData objcsv = newCSVData(); str todaydate; BaseDate todaydate1; int CountUpdate,CountInsert,CountUpdate1,CountInsert1; A_PurchaseOrder objMastertable; #File filepath = WinAPI::getOpenFileName( 0, confilter, "", "Save As", "", "Untitled") ; try { permission = new FileIoPermission(filepath, #io_read); permission.assert(); file = newTextIo(filepath, #io_read); if (!file) throw Exception::Error; file.inRecordDelimiter(#delimiterCRLF); file.inFieldDelimiter("|"); record = file.read(); while (file.status() == IO_Status::Ok) { objMastertable.Purchase_ID = Conpeek(record,1); //first column _PID = objMastertable.Purchase_ID; objMastertable.Vender_Code = Conpeek(record,2); //Second column _VID = objMastertable.Vender_Code; select * fromobjMastertablewhereobjMastertable.Purchase_ID == _PID; if(objMastertable.Purchase_ID) { ttsBegin; select forUpdate objMastertable where objMastertable.Purchase_ID == _PID && objMastertable.Vender_Code==_VID; objMastertable.Purchase_ID = Conpeek(record,1); //first column objMastertable.Vender_Code=Conpeek(record,2); //secon column objMastertable.Status=Conpeek(record,3); //third column objMastertable.Purchase_Date= str2Date(Conpeek(record,4),123);//forth column objMastertable.Purchase_Amount= Conpeek(record,5); //fifth column objMastertable.update(); CountUpdate++; record = file.read(); ttsCommit; } else { objMastertable.Purchase_ID = Conpeek(record,1); //first column objMastertable.Vender_Code=Conpeek(record,2); //secon column objMastertable.Status=Conpeek(record,3);//third column objMastertable.Purchase_Date=str2Date(any2str(Conpeek(record,4)),123); objMastertable.Purchase_Amount= Conpeek(record,5); //fifth column objMastertable.insert(); CountInsert++; record = file.read(); } } } catch(Exception::Error) { error("You do not have access to write the file to the selected folder"); } CodeAccessPermission::revertAssert(); Box::info(strFmt("%1 Update & %2 Insert",CountUpdate,CountInsert)); }
Thanks for comments.....