{"id":1586,"date":"2011-02-24T23:24:37","date_gmt":"2011-02-24T17:54:37","guid":{"rendered":"http:\/\/JitendraZaa.com\/blog\/?p=1586"},"modified":"2011-02-24T23:24:37","modified_gmt":"2011-02-24T17:54:37","slug":"read-file-present-inside-java-and-j2ee-project","status":"publish","type":"post","link":"https:\/\/www.jitendrazaa.com\/blog\/java\/servlet\/read-file-present-inside-java-and-j2ee-project\/","title":{"rendered":"Read File Present inside Java and J2EE Project"},"content":{"rendered":"<figure id=\"attachment_1587\" aria-describedby=\"caption-attachment-1587\" style=\"width: 249px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/02\/Read-File-Present-inside-Java-and-J2EE-Project.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1587\" title=\"Read File Present inside Java and J2EE Project\" src=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/02\/Read-File-Present-inside-Java-and-J2EE-Project.jpg?resize=249%2C361&#038;ssl=1\" alt=\"Read File Present inside Java and J2EE Project\" width=\"249\" height=\"361\" \/><\/a><figcaption id=\"caption-attachment-1587\" class=\"wp-caption-text\">Read File Present inside Java and J2EE Project<\/figcaption><\/figure>\n<p>Consider above project hierarchy, we want to read the file &#8220;hibernate.cfg.xml&#8221;\u009d in &#8220;TestMain.java&#8221;\u009d. If we try to get the absolute path using &#8220;<strong>(new File(&#8220;&#8221;)).getAbsolutePath()<\/strong>&#8220;\u009d then it will give the path of eclipse in case we are using eclipse or tomcat from where the application is running. This problem occurs mainly in file upload functionality.<br \/>\nBelow code can be used to solve this type of problem.<!--more--><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\npackage com.G2.servlets;\nimport java.io.BufferedReader;\nimport java.io.InputStream;\nimport java.io.InputStreamReader;\n\npublic class TestMain {\n\t\/**\n\t * @param args\n\t *\/\n\tpublic static void main(String&#x5B;] args) throws Exception {\n\t\tTestMain testMain = new TestMain();\n\t\tInputStream st = testMain.getClass().getResourceAsStream(&quot;\/hibernate.cfg.xml&quot;);\n\t\tBufferedReader in = new BufferedReader(new InputStreamReader(st));\n\t\tString line;\n\t\twhile((line = in.readLine()) != null)\n\t\t{\n\t\t\tSystem.out.println(line);\n\t\t}\n\t}\n}\n<\/pre>\n<p>Now consider below situation:<\/p>\n<figure id=\"attachment_1588\" aria-describedby=\"caption-attachment-1588\" style=\"width: 310px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/02\/Read-File-Present-inside-J2EE-Project.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1588\" title=\"Read File Present inside J2EE Project\" src=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/02\/Read-File-Present-inside-J2EE-Project.jpg?resize=310%2C383&#038;ssl=1\" alt=\"Read File Present inside J2EE Project\" width=\"310\" height=\"383\" \/><\/a><figcaption id=\"caption-attachment-1588\" class=\"wp-caption-text\">Read File Present inside J2EE Project<\/figcaption><\/figure>\n<p>You need to read the file present in folder &#8220;WebContent&#8221;\u009d. To read the file, we can use here <strong>Servlet Context<\/strong>.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\nInputStream st= getServletContext().getResourceAsStream(&quot;\/css\/style.css&quot;);\n\t\tBufferedReader in = new BufferedReader(new InputStreamReader(st));\n\t\tString line;\n\t\twhile((line = in.readLine()) != null)\n\t\t{\n\t\t\tSystem.out.println(line);\n\t\t}\n\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>How to Read File Present inside Java and J2EE Project<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"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,"jz_research_post":"","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[11],"tags":[329],"class_list":["post-1586","post","type-post","status-publish","format-standard","hentry","category-servlet","tag-java"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":1976,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/jsp\/j2ee-servlet-jsp-jsf-and-jms-version-table\/","url_meta":{"origin":1586,"position":0},"title":"J2EE &#8211; Servlet , JSP , JSF and JMS Version Table","author":"Jitendra","date":"April 13, 2011","format":false,"excerpt":"J2EE - Servlet , JSP , JSF and JMS Version Table","rel":"","context":"In &quot;JSP&quot;","block_context":{"text":"JSP","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/jsp\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1959,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/servlet\/create-servlet-using-annotation\/","url_meta":{"origin":1586,"position":1},"title":"Create Servlet using Annotation &#8211; Servlet 3.0","author":"Jitendra","date":"April 13, 2011","format":false,"excerpt":"Create the Servlet without any deployment descriptor. Using Annotations","rel":"","context":"In &quot;Servlet&quot;","block_context":{"text":"Servlet","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/servlet\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1494,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/servlet\/life-cycle-of-servlet\/","url_meta":{"origin":1586,"position":2},"title":"Life Cycle of Servlet","author":"Jitendra","date":"February 12, 2011","format":false,"excerpt":"Explain Life Cycle of Servlet","rel":"","context":"In &quot;Servlet&quot;","block_context":{"text":"Servlet","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/servlet\/"},"img":{"alt_text":"Servlet Life Cycle","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/02\/Servlet-Life-Cycle.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":2017,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/java-j2ee-interview-questions-1\/","url_meta":{"origin":1586,"position":3},"title":"Java &#8211; J2EE Interview Questions &#8211; 1","author":"Jitendra","date":"April 15, 2011","format":false,"excerpt":"JAVA - J2EE Interview Questions - 1, JAVA - J2EE Interview Questions - 1,custom tag JSP, Externalization , serialVersionUID, difference between interface and abstract class, iterate HashMap","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":5434,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/creating-custom-ant-plugin-or-task-in-java\/","url_meta":{"origin":1586,"position":4},"title":"Creating custom ANT plugin or Task in Java","author":"Jitendra","date":"March 19, 2016","format":false,"excerpt":"Tutorial to create custom Task in ANT with complete source code","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"Create custom ANT Task in Java","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2016\/03\/Creating-ANT-plugin.gif?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2016\/03\/Creating-ANT-plugin.gif?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2016\/03\/Creating-ANT-plugin.gif?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2016\/03\/Creating-ANT-plugin.gif?resize=700%2C400&ssl=1 2x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2016\/03\/Creating-ANT-plugin.gif?resize=1050%2C600&ssl=1 3x"},"classes":[]},{"id":2347,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/hibernate\/step-by-step-hibernate-tutorial-using-eclipse-wtp\/","url_meta":{"origin":1586,"position":5},"title":"Step By Step Hibernate Tutorial Using eclipse WTP","author":"Jitendra","date":"August 8, 2011","format":false,"excerpt":"Step By Step Hibernate (ORM Tool) Tutorial Using eclipse WTP","rel":"","context":"In &quot;Hibernate&quot;","block_context":{"text":"Hibernate","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/hibernate\/"},"img":{"alt_text":"Eclipse Install New Software - Hibernate","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/08\/Eclipse-Install-New-Software-Hibernate.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/1586","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/comments?post=1586"}],"version-history":[{"count":0,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/1586\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/media?parent=1586"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/categories?post=1586"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/tags?post=1586"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}