{"id":123,"date":"2017-05-26T22:06:05","date_gmt":"2017-05-26T22:06:05","guid":{"rendered":"https:\/\/leedavies.dev\/?p=123"},"modified":"2017-05-26T22:06:05","modified_gmt":"2017-05-26T22:06:05","slug":"reading-and-writing-delimited-files-in-net-core","status":"publish","type":"post","link":"https:\/\/leedavies.dev\/index.php\/2017\/05\/26\/reading-and-writing-delimited-files-in-net-core\/","title":{"rendered":"Reading and Writing Delimited Files in .Net Core"},"content":{"rendered":"<p>I often need to read and write files in delimited files from within a .Net Core application. It seems that there is no inbuilt parser for doing this so I wrote one.<\/p>\n<p>The code consists of a single static class \u201cDelimtedFile\u201d that has two methods, one to read a delimited file and one to write one.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nusing System;\r\nusing System.Collections.Generic;\r\nusing System.IO;\r\nusing System.Linq;\r\nusing System.Linq.Expressions;\r\nusing System.Text;\r\n\r\nnamespace ReadAndWriteDelimtedFiles\r\n{\r\n    public static class DelimtedFile\r\n    {\r\n        \/\/\/ &lt;summary&gt;\r\n        \/\/\/ Reads a delimited file into a List of strings lists.\r\n        \/\/\/ &lt;\/summary&gt;\r\n        \/\/\/ &lt;param name=&quot;filePath&quot;&gt;The path to the file&lt;\/param&gt;\r\n        \/\/\/ &lt;param name=&quot;delimter&quot;&gt;The delimiter used \r\n        \/\/\/ to parse the items within each line of the file.&lt;\/param&gt;\r\n        \/\/\/ &lt;returns&gt;&lt;\/returns&gt;\r\n        public static List&lt;List&lt;string&gt;&gt; ReadDelimtedFile(string filePath, \r\n            char delimter)\r\n        {\r\n            var lines = new List&lt;List&lt;string&gt;&gt;();\r\n\r\n            var fileLines = File.ReadAllLines(filePath);\r\n\r\n            foreach (var line in fileLines)\r\n            {\r\n                var lineList = new List&lt;string&gt;();\r\n                lineList.AddRange(line.Split(delimter));\r\n                lines.Add(lineList);\r\n            }\r\n\r\n            return lines;\r\n        }\r\n\r\n        \/\/\/ &lt;summary&gt;\r\n        \/\/\/ Writes the data in linesList into a \r\n        \/\/\/ output specificed by path using the delimiter.\r\n        \/\/\/ If headerList is not null inlcudes the header \r\n        \/\/\/ at the top of the file.\r\n        \/\/\/ &lt;\/summary&gt;\r\n        \/\/\/ &lt;param name=&quot;path&quot;&gt;The path to the file to \r\n        \/\/\/ write the delimited file.&lt;\/param&gt;\r\n        \/\/\/ &lt;param name=&quot;linesList&quot;&gt;The contents of the \r\n        \/\/\/ file to write out&lt;\/param&gt;\r\n        \/\/\/ &lt;param name=&quot;headerList&quot;&gt;The header line for \r\n        \/\/\/ the file, if null no header is included&lt;\/param&gt;\r\n        \/\/\/ &lt;param name=&quot;delimiter&quot;&gt;The delimiter for \r\n        \/\/\/ the items in linesList&lt;\/param&gt;\r\n        public static void WriteDelimtedFile(string path, \r\n            List&lt;List&lt;string&gt;&gt; linesList, \r\n            List&lt;string&gt; headerList,\r\n            char delimiter)\r\n        {\r\n            var lines = new List&lt;string&gt;();\r\n\r\n            if (headerList != null)\r\n            {\r\n                lines.Add(headerList.Aggregate((i, j) =&gt; i + delimiter + j));\r\n            }\r\n\r\n            lines.AddRange(linesList.Select(listString =&gt; \r\n                listString.Aggregate((i, j) =&gt; i + delimiter + j)));\r\n\r\n            File.WriteAllLines(path, lines.ToArray());\r\n        }\r\n    }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I often need to read and write files in delimited files from within a .Net Core application. It seems that there is no inbuilt parser for doing this so I wrote one. The code consists of a single static class \u201cDelimtedFile\u201d that has two methods, one to read a delimited file and one to write one. using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Linq.Expressions; using System.Text; namespace ReadAndWriteDelimtedFiles { public static class DelimtedFile { \/\/\/ &lt;summary&gt; \/\/\/ Reads&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/leedavies.dev\/index.php\/2017\/05\/26\/reading-and-writing-delimited-files-in-net-core\/\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20,21,2],"tags":[19,6],"class_list":["post-123","post","type-post","status-publish","format-standard","hentry","category-netcore","category-csharp","category-microsoft","tag-net-core","tag-microsoft"],"_links":{"self":[{"href":"https:\/\/leedavies.dev\/index.php\/wp-json\/wp\/v2\/posts\/123","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/leedavies.dev\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/leedavies.dev\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/leedavies.dev\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/leedavies.dev\/index.php\/wp-json\/wp\/v2\/comments?post=123"}],"version-history":[{"count":9,"href":"https:\/\/leedavies.dev\/index.php\/wp-json\/wp\/v2\/posts\/123\/revisions"}],"predecessor-version":[{"id":134,"href":"https:\/\/leedavies.dev\/index.php\/wp-json\/wp\/v2\/posts\/123\/revisions\/134"}],"wp:attachment":[{"href":"https:\/\/leedavies.dev\/index.php\/wp-json\/wp\/v2\/media?parent=123"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/leedavies.dev\/index.php\/wp-json\/wp\/v2\/categories?post=123"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/leedavies.dev\/index.php\/wp-json\/wp\/v2\/tags?post=123"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}