{"id":394,"date":"2013-12-03T05:47:43","date_gmt":"2013-12-03T05:47:43","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=394"},"modified":"2013-12-03T05:47:43","modified_gmt":"2013-12-03T05:47:43","slug":"how-to-read-a-file-in-java","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/how-to-read-a-file-in-java\/","title":{"rendered":"How to read a file in java"},"content":{"rendered":"<p>The below code reads a Sample.txt file and places the content in a newly created Html file <strong>Sample.html<\/strong>.<\/p>\n<p>The file from which the content should be read is placed in a project directory which is printed in the console using the below java code.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nSystem.out.println(&quot;Working Directory = &quot; + System.getProperty(&quot;user.dir&quot;));\r\n<\/pre>\n<p>The above code prints the current project directory in console.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage com.mugil.servlets;\r\n\r\nimport java.awt.Desktop;\r\nimport java.io.*;\r\n\r\nclass GenerateHTML \r\n{\r\n    public static void main(String&#x5B;] args) throws Exception \r\n    {    \t\r\n    \tSystem.out.println(&quot;Working Directory = &quot; +\r\n                System.getProperty(&quot;user.dir&quot;));\r\n        BufferedReader br = new BufferedReader(new FileReader(&quot;Sample.txt&quot;));\r\n        File f = new File(&quot;source.htm&quot;);\r\n        BufferedWriter bw = new BufferedWriter(new FileWriter(f));\r\n        bw.write(&quot;&lt;html&gt;&quot;);\r\n        bw.write(&quot;&lt;body&gt;&quot;);\r\n        bw.write(&quot;&lt;h1&gt;ShowGeneratedHtml source&lt;\/h1&gt;&quot;);\r\n\r\n        String line;\r\n        while ((line=br.readLine())!=null) {\r\n            bw.write(line);\r\n            bw.newLine();\r\n        }        \r\n        bw.write(&quot;&lt;\/body&gt;&quot;);\r\n        bw.write(&quot;&lt;\/html&gt;&quot;);\r\n\r\n        br.close();\r\n        bw.close();\r\n\r\n        Desktop.getDesktop().browse(f.toURI());\r\n    }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The below code reads a Sample.txt file and places the content in a newly created Html file Sample.html. The file from which the content should be read is placed in a project directory which is printed in the console using the below java code. System.out.println(&quot;Working Directory = &quot; + System.getProperty(&quot;user.dir&quot;)); The above code prints the&hellip; <a href=\"https:\/\/codethataint.com\/blog\/how-to-read-a-file-in-java\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[62],"tags":[],"class_list":["post-394","post","type-post","status-publish","format-standard","hentry","category-file-handling"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/394","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=394"}],"version-history":[{"count":1,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/394\/revisions"}],"predecessor-version":[{"id":395,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/394\/revisions\/395"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=394"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=394"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=394"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}