{"id":494,"date":"2010-05-14T04:04:00","date_gmt":"2010-05-14T04:04:00","guid":{"rendered":"https:\/\/opstree.com\/blog\/\/2010\/05\/14\/property-file-reader\/"},"modified":"2019-07-11T07:41:35","modified_gmt":"2019-07-11T07:41:35","slug":"property-file-reader","status":"publish","type":"post","link":"https:\/\/opstree.com\/blog\/2010\/05\/14\/property-file-reader\/","title":{"rendered":"Property file reader"},"content":{"rendered":"<p>In my current project we have some property files and we need to read properties from those property files, so we come up with an approach to read property files. The main points that we need to focus was<br \/>\n1.) The properties should be loaded only once, In a way we need singleton behavior<br \/>\n2.) We can have multiple property files<br \/>\nOne assumption that we have is property files will always be located in root class path<\/p>\n<p>Now for the solution of this problem we thought of using Enum (effective java ;)) as they internally provide the feature of singleton behavior, also for multiple property file situation we created multiple instances corresponding property file.<\/p>\n<p>So the code that we have written is like this<\/p>\n<pre style=\"font-family:Andale Mono, Lucida Console, Monaco, fixed, monospace;color:#000000;background-color:#eee;font-size:12px;border:1px dashed #999999;line-height:14px;padding:5px;overflow:auto;width:100%;\"><code>\npublic enum PropertiesReader {\n     SEARCH(\"search.properties\"),APP(\"app.properties\");\n\n     Properties properties;\n     private Logger log = LoggerFactory.getLogger(PropertiesReader.class);\n\n     private PropertiesReader(String propertyFile) {\n   properties = new Properties();\n          try {\n               InputStream inputStream =    this.getClass().getClassLoader().getResourceAsStream(propertyFile);\n               properties.load(inputStream);\n          }\n          catch (IOException e) {\n               log.error(propertyFile + \" was not found in classpath\");\n          }\n   }\n\n     public String getValue(String key) {\n          return (String) properties.get(key);\n     }\n}\n<\/code>\n<\/pre>\n<p>So now<br \/>\nif we want to read a property from search.properties file<br \/>\nassertThat(PropertiesReader.SEARCH.getValue(&#8220;max.pages.to.display&#8221;)<\/p>\n<p>if we want to read a property from app.properties file<br \/>\nassertThat(PropertiesReader.APP.getValue(&#8220;debug_mode&#8221;)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In my current project we have some property files and we need to read properties from those property files, so we come up with an approach to read property files. The main points that we need to focus was 1.) The properties should be loaded only once, In a way we need singleton behavior 2.) &hellip; <a href=\"https:\/\/opstree.com\/blog\/2010\/05\/14\/property-file-reader\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Property file reader&#8221;<\/span><\/a><\/p>\n","protected":false},"author":150552946,"featured_media":29900,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":"","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"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},"version":2}},"categories":[1],"tags":[2512127,106916,13171797],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/opstree.com\/blog\/wp-content\/uploads\/2025\/11\/DevSecOps-1.jpg","jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/pfDBOm-7Y","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/posts\/494"}],"collection":[{"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/users\/150552946"}],"replies":[{"embeddable":true,"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/comments?post=494"}],"version-history":[{"count":2,"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/posts\/494\/revisions"}],"predecessor-version":[{"id":955,"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/posts\/494\/revisions\/955"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/media\/29900"}],"wp:attachment":[{"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/media?parent=494"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/categories?post=494"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/tags?post=494"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}