{"id":233,"date":"2019-05-17T09:49:31","date_gmt":"2019-05-17T08:49:31","guid":{"rendered":"https:\/\/www.bowlerblue.com\/site\/?p=233"},"modified":"2019-05-17T10:15:08","modified_gmt":"2019-05-17T09:15:08","slug":"java-read-contents-of-resource-file-to-string","status":"publish","type":"post","link":"https:\/\/www.bowlerblue.com\/site\/java-read-contents-of-resource-file-to-string\/","title":{"rendered":"Java: Read contents of resource file to String"},"content":{"rendered":"\n<p><img loading=\"lazy\" decoding=\"async\" width=\"75\" height=\"75\" class=\"wp-image-110\" style=\"width: 75px;\" src=\"https:\/\/www.bowlerblue.com\/site\/wp-content\/uploads\/2018\/08\/invoice.png\" alt=\"\" srcset=\"https:\/\/www.bowlerblue.com\/site\/wp-content\/uploads\/2018\/08\/invoice.png 512w, https:\/\/www.bowlerblue.com\/site\/wp-content\/uploads\/2018\/08\/invoice-150x150.png 150w, https:\/\/www.bowlerblue.com\/site\/wp-content\/uploads\/2018\/08\/invoice-300x300.png 300w\" sizes=\"auto, (max-width: 75px) 100vw, 75px\" \/><\/p>\n\n\n\n<p>If you have a resource text file &#8211; even if packaged in a JAR file &#8211; you can use the following Java 8(+) code to read the contents into a String.  This code will also work in a static method (hence the ClassLoader.getSystemClassLoader()).  If you&#8217;re in a normal method you can use the current instance&#8217;s classloader instead.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>String contents = \"\";\nClassLoader classLoader = ClassLoader.getSystemClassLoader();\nInputStream inputStream = classLoader.getResourceAsStream(filename);\nif (inputStream != null) {\n        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));\n        contents = reader.lines().collect(Collectors.joining(System.lineSeparator()));\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>If you have a resource text file &#8211; even if packaged in a JAR file &#8211; you can use the following Java 8(+) code to read the contents into a String. This code will also work in a static method (hence the ClassLoader.getSystemClassLoader()). If you&#8217;re in a normal method you can use the current instance&#8217;s [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[9,6,7,10,8],"class_list":["post-233","post","type-post","status-publish","format-standard","hentry","category-code","tag-coding","tag-dev","tag-java","tag-java8","tag-programming"],"_links":{"self":[{"href":"https:\/\/www.bowlerblue.com\/site\/wp-json\/wp\/v2\/posts\/233","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bowlerblue.com\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bowlerblue.com\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bowlerblue.com\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bowlerblue.com\/site\/wp-json\/wp\/v2\/comments?post=233"}],"version-history":[{"count":2,"href":"https:\/\/www.bowlerblue.com\/site\/wp-json\/wp\/v2\/posts\/233\/revisions"}],"predecessor-version":[{"id":236,"href":"https:\/\/www.bowlerblue.com\/site\/wp-json\/wp\/v2\/posts\/233\/revisions\/236"}],"wp:attachment":[{"href":"https:\/\/www.bowlerblue.com\/site\/wp-json\/wp\/v2\/media?parent=233"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bowlerblue.com\/site\/wp-json\/wp\/v2\/categories?post=233"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bowlerblue.com\/site\/wp-json\/wp\/v2\/tags?post=233"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}