{"id":3481,"date":"2016-03-18T00:48:07","date_gmt":"2016-03-18T00:48:07","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=3481"},"modified":"2016-03-18T00:48:07","modified_gmt":"2016-03-18T00:48:07","slug":"scala-read-file-memory","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/scala-read-file-memory\/","title":{"rendered":"Scala: read a file into memory"},"content":{"rendered":"<p>To read in an entire file in Scala, you can use Java APIs, but by initializing data structures in a different way &#8211; note how the byte array is created.<\/p>\n<pre lang=\"scala\">\ntry {\n  val file = new File(\"src\/main\/scala\/Main.scala\")\n  val fis = new FileInputStream(file)\n\n  val data = Array.fill[Byte](file.length.toInt)(0)\n  fis.read(data)\n  fis.close\n\n  val str = new String(data, \"UTF-8\")\n\n  print(str)\n} catch {\n  case e: Exception => {\n    println(e)\n  }\n}\n<\/pre>\n<p>Also of interest is the exception block &#8211; this uses pattern matching, which makes it look more like a case statement in Java.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>To read in an entire file in Scala, you can use Java APIs, but by initializing data structures in a different way &#8211; note how the byte array is created. try { val file = new File(&#8220;src\/main\/scala\/Main.scala&#8221;) val fis = new FileInputStream(file) val data = Array.fill[Byte](file.length.toInt)(0) fis.read(data) fis.close val str = new String(data, &#8220;UTF-8&#8221;) print(str) &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.garysieling.com\/blog\/scala-read-file-memory\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Scala: read a file into memory&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4],"tags":[480,485],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/3481"}],"collection":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/comments?post=3481"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/3481\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=3481"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=3481"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=3481"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}