Thursday, June 5, 2014

read and write data from text (.txt) file

// read and write data from text (.txt) file with AsciiIo

Method:

AsciiIo readFile;
str line;
container fileRecord;
;

readFile = new AsciiIo("C:\\test.txt" , 'R');
readFile.inFieldDelimiter("1234567890abcdefghijklmnop");

fileRecord = readFile.read();
while (fileRecord)
{
line = con2str(fileRecord);
info(line);
fileRecord = readFile.read();
}


// Read Data

#File
    TextIo                  io;
    //NotepadTest     notepadTest;
    container               c;
    str               item, name;
    ;
 
 
    io = new TextIo("C:\\Test.txt", #io_read);
 
    if (!io)
    {
        throw error("Error reading file");
    }
 
    io.inFieldDelimiter(','); //This can be any delimiter, I'm just using Semicolon as an example
    io.inRecordDelimiter(#delimiterCRLF); //CRLF = Carriage Return Line Feed
 
    ttsbegin;
    delete_from NotepadTest;
    while (io.status() == IO_Status::Ok)
    {
        c = io.read();
        if (io.status() != IO_Status::Ok)
        {
            break;
        }
        item = conpeek(c, 1);
        NotepadTest.ItemId = item;
        name=  conpeek(c, 2);
        NotepadTest.ItemName  = name;
     
        NotepadTest.insert();
        info(item + '-->' + name);
    }
    ttscommit;
 
    super();
    NotepadTest_DS.research();
    NotepadTest_DS.reread();
    NotepadTest_DS.refresh();


// Write Data

    //NotepadTest     notepadTest;
    BinData     binData;
    TextBuffer  textBuffer;
    Dialog      dialog;
    DialogGroup dialogGroup;
    DialogField dialogfieldItmeid;
    str 100 itemDialog;
    ;
    dialog = new Dialog("ItemId");
    dialogGroup = dialog.addGroup("Item Group");
    dialogfieldItmeid = dialog.addField(extendedTypeStr(Name),"Itemid > = :");
 
    textBuffer = new TextBuffer();
    textBuffer.setText('');
        if(dialog.run())
        {
            itemDialog = dialogfieldItmeid.value();
           // info(strfmt("%1", itemDialog));
         
            while select notepadTest where notepadTest.ItemId >= itemDialog
            {
                textBuffer.appendText(strfmt('%1\r\t',notepadTest.ItemId));
                textBuffer.appendText(strfmt('%1\r\n',notepadTest.ItemName));
            }
        }
    textBuffer.getText();
    NotepadTest_DS.research();
    NotepadTest_DS.reread();
    NotepadTest_DS.refresh();
    binData = new BinData();
    binData.setStrData(textBuffer.getText());
    binData.saveFile(@"c:\Test1.txt");

    super();

No comments:

Post a Comment

Convert Call stack to readable format in D365FO X++

//Input --container _xppCallStack = xSession::xppCallStack();  Public static str POL_formatXppCallStack(container _xppCallStack, int _skipFr...