How to Read and Write Text using CommaTextIO in Dynamics Ax



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.

CommaTextIO

CommaTextIO class is good for export kind of work where we need comma delimited or any other character delimited file format. You can specify field delimiter as well as record delimiter. A record is sent for writing as a container. All the values in the container are written with fields separated by the delimiter character specified and a record delimiter is written after the last value written from the container.

Write in Text Using CommaTextIO

static void ExportinTextCommnIO(Args _args)
{
    CommaIo file;
    container line;
    str filepath;
    containerconfilter =['.txt','*.txt'];
    A_PurchaseOrder objA_PurchaseOrder;
    FileIoPermission permission;
    filepath = WinAPI::getSaveFileName(
        0,
        confilter,
        "",
        "Save As",
        "",
        "Untitled")
    #File
    ;
    permission = new FileIoPermission(filepath, #io_write);
    permission.assert();
    file = new CommaIo(filepath, #io_write);
    file.outRecordDelimiter(#delimiterCRLF);
    file.outFieldDelimiter(' | ');// for semicolon seperator
    //file = new CommaIo(filepath, #io_write);
    if (!file || file.status() != IO_Status::Ok)
    {
        throw error("File cannot be opened.");
    }
    while select objA_PurchaseOrder
    {
        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);
        file.writeExp(line);
    }
    CodeAccessPermission::revertAssert();
}

Read in Text Using CommaTextIO

static voidT estCommaTextIO(Args _args)
{
    #File
    CommaTextIo commaTextIo;
    FileIOPermission   permission;
    container containFromRead;
    str line;
    str filepath;
    container confilter =['.txt','*.txt'];
    int     x;
    int     cols;
    ;
    filepath = WinAPI::getOpenFileName(
        0,
        confilter,
        "",
        "Save As",
        "",
        "Untitled");
    permission = new FileIOPermission(filepath,#io_read);
    permission.assert();

    commaTextIo = new CommaTextIO(filepath,'R');

    containFromRead = commaTextIo.read();
    While(containFromRead)
    {
        //cols = conLen(containFromRead);
        //for(x=1;x<=cols;x++)
        //{
            //print conpeek(containFromRead,x);
            line = con2str(containFromRead);
            info(line);
            containFromRead = commaTextIo.read();
        //}
        //containFromRead = commaTextIo.read();
    }
    //pause;
    commaTextIo = null;
}

Related Posts

Previous
Next Post »

Thanks for comments.....