最新消息:ww12345678 的部落格重装上线,希望大家继续支持。

[转]Microsoft Dynamics AX 2012 Performing File IO with the TextIo Class

AX 2012 William 3681浏览 0评论

原文地址:http://msdn.microsoft.com/en-us/library/cc967403.aspx

Updated: November 18, 2010

Applies To: Microsoft Dynamics AX 2012 Feature Pack, Microsoft Dynamics AX 2012

X++ code performs file input and output (IO) by using the TextIo class. TextIo uses Unicode.


X++ Sample


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.The use of the FileIOPermission class is also illustrated. FileIoPermission is used to assert that the current method has the authority to call another method that checks for such authority. For more information, see Code Access Security.


X++


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.
    txIoRead = 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; X++)
        {
            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();
}


X++ Sample Output


Message (14:12:47)
File is at: C:DOCUME~1myaliasLOCALS~1TempTest_File_IO.txt
Hello World.
The sky is blue.

// EOFile

转载请注明:ww12345678 的部落格 | AX Helper » [转]Microsoft Dynamics AX 2012 Performing File IO with the TextIo Class

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址