{"id":2724,"date":"2017-11-27T08:18:41","date_gmt":"2017-11-27T08:18:41","guid":{"rendered":"http:\/\/webartdevelopers.com\/?p=2724"},"modified":"2017-11-27T08:18:41","modified_gmt":"2017-11-27T08:18:41","slug":"how-to-read-from-the-file-using-fileinputstream-class-in-core-java","status":"publish","type":"post","link":"https:\/\/webartdevelopers.com\/blog\/how-to-read-from-the-file-using-fileinputstream-class-in-core-java\/","title":{"rendered":"How to Read from the File using FileInputStream class in Core Java"},"content":{"rendered":"<p>In this tutorial how to read from the file using FileInputStream class in Core Java is shown.<\/p>\n<p>Code:<\/p>\n<p>FileInputStreamDemo.java<\/p>\n<p>import java.io.*;<\/p>\n<p>class Test4<br \/>\n{<br \/>\npublic static void main(String[] args)<br \/>\n{<br \/>\nString fileName=&#8221;temp1.txt&#8221;;<\/p>\n<p>try<br \/>\n{<br \/>\nbyte[] buffer = new byte[1000];<br \/>\nFileInputStream inputStream = new FileInputStream(fileName);<br \/>\nint total = 0;<br \/>\nint nRead = 0;<\/p>\n<p>while((nRead = inputStream.read(buffer))!= -1)<br \/>\n{<br \/>\nSystem.out.println(new String(buffer));<br \/>\ntotal += nRead;<br \/>\n}<br \/>\ninputStream.close();<br \/>\nSystem.out.println(&#8220;Read &#8220;+total+&#8221; bytes&#8221;);<br \/>\n}<br \/>\ncatch(FileNotFoundException ex)<br \/>\n{<br \/>\nSystem.out.println(&#8220;Unable to open file &#8216;&#8221;+fileName+&#8221;&#8216;&#8221;);<br \/>\n}<br \/>\ncatch(IOException ex)<br \/>\n{<br \/>\nSystem.out.println(&#8220;Error readinf file &#8216;&#8221;+fileName+&#8221;&#8216;&#8221;);<br \/>\n}<br \/>\n}<br \/>\n}<\/p>\n<p><a href=\"https:\/\/youtu.be\/K20PDBdB6Wo\">https:\/\/youtu.be\/K20PDBdB6Wo<\/a><\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"In this tutorial how to read from the file using FileInputStream class in Core Java is shown. Code: FileInputStreamDemo.java import java.io.*; class Test4 { public static void main(String args) { String fileName=&#8221;temp1.txt&#8221;; try { byte buffer = new byte; FileInputStream inputStream = new FileInputStream(fileName); int total = 0; int nRead = 0; while((nRead = inputStream.read(buffer))!= -1) { System.out.println(new String(buffer)); total += nRead; } inputStream.close(); System.out.println(&#8220;Read &#8220;+total+&#8221; bytes&#8221;); } catch(FileNotFoundException ex) { System.out.println(&#8220;Unable to open file &#8216;&#8221;+fileName+&#8221;&#8216;&#8221;); } catch(IOException ex) { System.out.println(&#8220;Error readinf file &#8216;&#8221;+fileName+&#8221;&#8216;&#8221;); } } }<!-- AddThis Advanced Settings generic via filter on get_the_excerpt -->","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[156],"tags":[157,203,199],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/posts\/2724"}],"collection":[{"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/comments?post=2724"}],"version-history":[{"count":2,"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/posts\/2724\/revisions"}],"predecessor-version":[{"id":2726,"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/posts\/2724\/revisions\/2726"}],"wp:attachment":[{"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/media?parent=2724"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/categories?post=2724"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/tags?post=2724"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}