{"id":822,"date":"2009-03-06T16:01:17","date_gmt":"2009-03-06T22:01:17","guid":{"rendered":"http:\/\/www.cyberward.net\/blog\/?p=822"},"modified":"2009-03-06T16:01:17","modified_gmt":"2009-03-06T22:01:17","slug":"loading-java-resources-correctly","status":"publish","type":"post","link":"https:\/\/www.cyberward.net\/blog\/2009\/03\/loading-java-resources-correctly\/","title":{"rendered":"Loading Java Resources Correctly"},"content":{"rendered":"<p>I am currently creating a continuation of the java series on useing reflection. In one of the examples I was trying to read from a properties file. I completely forgot how to find the resource I was looking for. I found <a href=\"http:\/\/www.javaworld.com\/javaworld\/javaqa\/2003-08\/01-qa-0808-property.html\">this<\/a> article on Java World. It&#8217;s from 2003, but still relevent.<\/p>\n<p>To start with. The myFile.properties file is in the same package as the class MyClass.<\/p>\n<p>These were my attempts:<\/p>\n<pre lang=\"java\" colla=\"+\">\r\n\/\/ 1. Just get a FileInputStream\r\nInputStream aStream = new FileInputStream(\"myFile.properties\");\r\n\r\n\/\/ 2. Switched to getResourceAsStream\r\nInputStream aStream = MyClass.class.getClassLoader().getResourceAsStream(\"my\/package\/myProps.properties\");\r\n\r\n\/\/ 3. Settled on \r\nInputStream aStream = MyClass.class.getResourceAsStream(\"myProps.properties\");\r\n<\/pre>\n<p>So, the first attempt was me completely forgetting how to do things. The only way that works is if you are putting in an absolute path from the file system. Never a good idea.<\/p>\n<p>The second one, using the ClassLoader went astray because I forgot to include the &#8220;\/&#8221; at the beginning.<\/p>\n<p>The third times a charm. If you get the resource right from the class, you can also use relative pathing, and I got to drop the path. <\/p>\n<p>That works much better. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>I am currently creating a continuation of the java series on useing reflection. In one of the examples I was trying to read from a properties file. I completely forgot how to find the resource I was looking for. I &hellip; <a href=\"https:\/\/www.cyberward.net\/blog\/2009\/03\/loading-java-resources-correctly\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[101],"tags":[150],"series":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/pffAy-dg","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/www.cyberward.net\/blog\/wp-json\/wp\/v2\/posts\/822"}],"collection":[{"href":"https:\/\/www.cyberward.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cyberward.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cyberward.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cyberward.net\/blog\/wp-json\/wp\/v2\/comments?post=822"}],"version-history":[{"count":0,"href":"https:\/\/www.cyberward.net\/blog\/wp-json\/wp\/v2\/posts\/822\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.cyberward.net\/blog\/wp-json\/wp\/v2\/media?parent=822"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cyberward.net\/blog\/wp-json\/wp\/v2\/categories?post=822"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cyberward.net\/blog\/wp-json\/wp\/v2\/tags?post=822"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/www.cyberward.net\/blog\/wp-json\/wp\/v2\/series?post=822"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}