AX读写文件

 

AX读写文件需要一些几步

备注

参考

AX读写文件需要以下几步:

(1)     创建句柄

client public static int createFile(str  fileName, [int  flags, int  access])

如要给文件路径为“d:/test.txt“的文件创建一个句柄,如下:

Int handle;

Str filename;

;

Filename = “d://test.txt”;

If(WinApi::fileExists(filename))

{

         Handle = WinAPI::createFile(filename);

}

(2)     获取文件流对象,并进行读写操作

AX系统提供了几种IO类可以进行文件的操作:

AsciiIO:读写ASCII文件

TextIO:读写TEXT文件

BinaryIO:读写二进制文件

commaIO:读写用逗号为分隔符的文件

每种类型的文件操作大致都相同,以TextIO为例,首先创建文件流对象:

public void new(str  filename, str  mode, [int codepage])

参数

Description

filename

要打开文件的文件名

mode

The mode in which the file should be opened. Specify the following:

·         R – Read

·         W – Write

·         A – Append (implies "W")

·         T – Translate (text)

·         B – Binary

codepage

The code page number for the character set to be read from/written to the file. The default value is 1200 (UTF-16LE).

Following are the most common values:

·         0 – CP_ACP; the ANSI code page

·         437 – OEM code page 437

·         850 – Code page 850

·         1200 – UTF-16LE

·         1201 – UTF-16BE

·         65001 – UTF-8

·         54936: GB-18030 

An error is reported if an invalid code page is requested. Notice that a code page is reported as "invalid" if it is not installed on the computer (different code pages may be installed on the client and server). For example, specifying code page 1253 for Greek fails unless the computer's ACP is Greek or Greek language support has been loaded by using Control Panel > Regional Options.

为了对一个文件进行重新写入,执行如下代码:

TextIO file = new TextIO(“D://test.txt”,’w’);

对象创建好了,但我们不能保证创建是成功的,TextIO给我们提供了函数status来判断我们前一操作的完成情况,如判断文件是否存在及前一操作是否成功,可以使用如下:

protected void checkDiskFileStatus()

{

    if (!diskFile || diskFile.status() != IO_Status::Ok)

    {

        throw error(strfmt("@SYS76826", diskFileName));

    }

}

接下来该对文件进行写入了,但在写文件前我们应该设置好字段间、及记录间的分隔符,也可以使用默认。

Public str outFieldDelimiter([str value]) 用于设置或读取字段分隔符,当你用一个container向文件写数据是,container内的数据就会有字段分隔符来分隔,如container c = [a,b,c,d],设置分隔符为”*”,则使用file.write(c)写入后文件后为 a*b*c*d

Public str outRecordDelimiter([str value]) 用于设置和读取记录分隔符,如记录分隔符设置为”-“,执行file.write(“aaa”);file.write(“bbb”)后写入文件结果是:aaa-bbb

以下代码将记录分隔符设置为tab加回车,将字段间设置为无分隔符:

void defineFile()

{

    diskFile = new TextIo(diskFileName,'W');

    ;

    if (!diskFile)

    {

        throw error("@SYS26757");

    }

    diskFile.outRecordDelimiter('/r/n');

    diskFile.outFieldDelimiter('');

}

用于写入的方法有:

write

Public boolean write(values)

Overridden. Writes data to a file represented by a TextIO object. 写入成功返回true,否则返回false

writeChar

Public boolean writeChar(int _int)

Writes a Unicode character to a file. 可以使用char2num(str _str,int index)来写入, file.writeChar(char2num(“Abc”,1)写入的是Afile.writeChar(char2num(“Abc”,1)写入的是b。写入成功返回true,否则返回false

writeExp

Public boolean writeExp(container _container)

Overridden. Writes the contents of a container to a file represented by a TextIO object. 写入成功返回true,否则返回false

 

 

(3)     读写文件
以写为例:
file.write(data) 
写入待写数据
file.writeExp(container) 
将所有的数据插入container,然后一次性写入。对于大数据量效率较高
文件格式问题:
在写入数据时,客户常常会有一些格式上的要求,例如:字符串长度不够时,需要补0,或者空格
0为例,操作如下:
str 10 strWrite = "0000000000";
然后用strPoke(str1,str2,int position)函数用待写入数据替换strWrite中的部分0
str 10 strwrite;
str strinput;
或者用strrep(" ",int number)重复空格 次数number
strwrite = strrep(" ",10 - strlen(strinput)) + strinput;

补充:对字符串进行操作时,先用strltrim()或者strrtrim()去掉字符串的左(右)空格,不是必须,但有时会避免不必要的problem

(4)     释放对象
WinAPI::colseHandle(handle);

 

备注:

1)使用TextIo读写文件的一个例子:

static void Job_File_IO_TextIo_Write_Read(Args _args)

{

    TextIo txIoRead,

         txIoWrite;

    container containFromRead;

    int xx,

        iConLength;

    str sTempPath,

        sFileName = "Test_File_IO.txt",

        sOneRecord;

           FileIoPermission fioPermission;

    ;

    // 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; xx++)

        {

            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();

}

2)也可以使用TextBuffer读写文件:

static void example()

{

    FileIoPermission _perm = new FileIoPermission("myfile.txt",'r');

    TextBuffer txtb = new TextBuffer();

    _perm.assert();

    txtb.fromFile("myfile.txt"); // Read text from file

    txtb.toClipboard(); // Copy it to the clipboard

}

 

又如:

fileIOPermission = new FileIOPermission("c:/david/hhcLog.txt",'r');

    textBuffer = new TextBuffer();

    fileIOPermission.assert();

    textBuffer.fromFile("c:/david/hhcLog.txt");

    tmp = textBuffer.getText();

 

    //info(int2str(textBuffer.numLines()));

    con = str2con(tmp,num2char(10));

    for(i=0;i<conlen(con);i++)

    {

        tmp = conpeek(con,i);

        info(tmp);

    }

    CodeAccessPermission::revertAssert();

 

参考:

TextBuffer

CommaTextIO

BinaryIO

FileIOPermission

AsciiIO

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值