{"id":1094,"date":"2022-12-09T15:27:18","date_gmt":"2022-12-09T14:27:18","guid":{"rendered":"http:\/\/daxvisionerp.com\/?p=1094"},"modified":"2025-10-22T14:33:07","modified_gmt":"2025-10-22T14:33:07","slug":"read-write-import-export-text-csv-ascii-file","status":"publish","type":"post","link":"https:\/\/daxvisionerp.com\/home\/read-write-import-export-text-csv-ascii-file\/","title":{"rendered":"Read and write file example"},"content":{"rendered":"\n<p>Here I share a simple example for exporting data in ASCII &#8216;csv&#8217; file format. The file is saved to the folder that is set up in the web browser.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    public static void main(Args _args)\n    {\n       Filename fileName = 'Filename.Txt';\n        AsciiStreamIo asciiIo = AsciiStreamIo::constructForWrite();\n\n        asciiIo.outRecordDelimiter(\"\\r\\n\");\n        asciiIo.outFieldDelimiter(\";\");\n\n        asciiIo.write('Line 1');\n        asciiIo.write('Line 2');\n        File::SendFileToUser(asciiIo.getStream(),'asciiFile.txt');\n    }<\/code><\/pre>\n\n\n\n<p>Here is another example code. This one is reading \/ importing data in ASCII &#8216;csv&#8217; file format. The csv file can be selected by the user from his or her local disk.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public static void main(Args _args)\n{\n    FileUploadTemporaryStorageResult    uploadFileResult = File::GetFileFromUser();\n    System.IO.Stream                    readStream = File::UseFileFromURL(uploadFileResult.getDownloadUrl());\n    AsciiStreamIo                       inFile = AsciiStreamIo::constructForRead(readStream);\n\n    container               readCon;\n    str                     recordRead;\n    int                     nRead;\n\n    inFile.inFieldDelimiter(\"\\r\\n\");\n    inFile.inRecordDelimiter(\"\\r\\n\");\n\n    if (!inFile)\n    {\n        \/\/FileName = uploadFileResult.getFileName();\n        Error('Cannot open file. Import failed.');\n    }\n    else\n    {\n        readCon = inFile.read();\n        while (inFile.status() == IO_Status::OK)\n        {\n            recordRead = conPeek(readCon, 1);\n            info(recordRead);\n            readCon = inFile.read();\n            nRead++;\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/daxvisionerp.com\/home\/wp-content\/uploads\/2022\/12\/image.png\"><img decoding=\"async\" src=\"https:\/\/daxvisionerp.com\/home\/wp-content\/uploads\/2022\/12\/image.png\" alt=\"\" class=\"wp-image-1106\"\/><\/a><figcaption class=\"wp-element-caption\">Browse file dialog displayed by File::GetFileFromUser()<\/figcaption><\/figure>\n\n\n\n<p>You can paste these codes to a runnable class and it will work or copy from <a href=\"https:\/\/github.com\/PeterProkopecz\/AX\/tree\/master\/D365FO_FileReadWrite\" target=\"_blank\" rel=\"noopener\">https:\/\/github.com\/PeterProkopecz\/AX\/tree\/master\/D365FO_FileReadWrite<\/a>. You can customize them to fit your needs. Use &#8216;TextStreamIo&#8217; class instead of &#8216;AsciiStreamIo&#8217; in case you want to use normal text files.<\/p>\n\n\n<div class=\"taxonomy-post_tag wp-block-post-terms\"><span class=\"wp-block-post-terms__prefix\"><br><\/span><a href=\"https:\/\/daxvisionerp.com\/home\/tag\/ascii\/\" rel=\"tag\">ascii<\/a><span class=\"wp-block-post-terms__separator\">, <\/span><a href=\"https:\/\/daxvisionerp.com\/home\/tag\/browse-file\/\" rel=\"tag\">browse file<\/a><span class=\"wp-block-post-terms__separator\">, <\/span><a href=\"https:\/\/daxvisionerp.com\/home\/tag\/csv-file\/\" rel=\"tag\">csv file<\/a><span class=\"wp-block-post-terms__separator\">, <\/span><a href=\"https:\/\/daxvisionerp.com\/home\/tag\/export-file\/\" rel=\"tag\">export file<\/a><span class=\"wp-block-post-terms__separator\">, <\/span><a href=\"https:\/\/daxvisionerp.com\/home\/tag\/import-file\/\" rel=\"tag\">import file<\/a><span class=\"wp-block-post-terms__separator\">, <\/span><a href=\"https:\/\/daxvisionerp.com\/home\/tag\/msdyn365fo-2\/\" rel=\"tag\">MSDyn365FO<\/a><span class=\"wp-block-post-terms__separator\">, <\/span><a href=\"https:\/\/daxvisionerp.com\/home\/tag\/read-file\/\" rel=\"tag\">read file<\/a><span class=\"wp-block-post-terms__separator\">, <\/span><a href=\"https:\/\/daxvisionerp.com\/home\/tag\/text-file\/\" rel=\"tag\">text file<\/a><span class=\"wp-block-post-terms__separator\">, <\/span><a href=\"https:\/\/daxvisionerp.com\/home\/tag\/write-file\/\" rel=\"tag\">write file<\/a><\/div>","protected":false},"excerpt":{"rendered":"<p>Here I share a simple example for exporting data in ASCII &#8216;csv&#8217; file format. The file is saved to the folder that is set up in the web browser. Here is another example code. This one is reading \/ importing data in ASCII &#8216;csv&#8217; file format. The csv file can be selected by the user [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"1080","footnotes":""},"categories":[19],"tags":[157,159,161,163,165,151,167,169,171],"class_list":["post-1094","post","type-post","status-publish","format-standard","hentry","category-dynamics-365fo","tag-ascii","tag-browse-file","tag-csv-file","tag-export-file","tag-import-file","tag-msdyn365fo-2","tag-read-file","tag-text-file","tag-write-file"],"_links":{"self":[{"href":"https:\/\/daxvisionerp.com\/home\/wp-json\/wp\/v2\/posts\/1094","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/daxvisionerp.com\/home\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/daxvisionerp.com\/home\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/daxvisionerp.com\/home\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/daxvisionerp.com\/home\/wp-json\/wp\/v2\/comments?post=1094"}],"version-history":[{"count":2,"href":"https:\/\/daxvisionerp.com\/home\/wp-json\/wp\/v2\/posts\/1094\/revisions"}],"predecessor-version":[{"id":1343,"href":"https:\/\/daxvisionerp.com\/home\/wp-json\/wp\/v2\/posts\/1094\/revisions\/1343"}],"wp:attachment":[{"href":"https:\/\/daxvisionerp.com\/home\/wp-json\/wp\/v2\/media?parent=1094"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/daxvisionerp.com\/home\/wp-json\/wp\/v2\/categories?post=1094"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/daxvisionerp.com\/home\/wp-json\/wp\/v2\/tags?post=1094"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}