{"id":1288,"date":"2022-02-26T07:49:06","date_gmt":"2022-02-26T07:49:06","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=1288"},"modified":"2022-02-26T07:49:06","modified_gmt":"2022-02-26T07:49:06","slug":"file-input-stream-class","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/02\/26\/file-input-stream-class\/","title":{"rendered":"File Input Stream Class"},"content":{"rendered":"\n<p>In this tutorial, we will learn about Java FileInputStream and its methods with the help of examples.<\/p>\n\n\n\n<p id=\"introduction\">The&nbsp;<code>FileInputStream<\/code>&nbsp;class of the&nbsp;<code>java.io<\/code>&nbsp;package can be used to read data (in bytes) from files.<\/p>\n\n\n\n<p>It extends the&nbsp;<code>InputStream<\/code>&nbsp;abstract class.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.programiz.com\/sites\/tutorial2program\/files\/java-fileinputstream.png\" alt=\"Java FileInputStream is a subclass of InputStream class.\" title=\"Java FileInputStream Class\"\/><\/figure>\n\n\n\n<p>Before we learn about\u00a0<code>FileInputStream<\/code>, make sure to know about\u00a0Java Files.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"create\">Create a FileInputStream<\/h2>\n\n\n\n<p>In order to create a file input stream, we must import the&nbsp;<code>java.io.FileInputStream<\/code>&nbsp;package first. Once we import the package, here is how we can create a file input stream in Java.<\/p>\n\n\n\n<p><strong>1. Using the path to file<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>FileInputStream input = new FileInputStream(stringPath);\n<\/code><\/pre>\n\n\n\n<p>Here, we have created an input stream that will be linked to the file specified by the&nbsp;<var>path<\/var>.<\/p>\n\n\n\n<p><strong>2. Using an object of the file<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>FileInputStream input = new FileInputStream(File fileObject);\n<\/code><\/pre>\n\n\n\n<p>Here, we have created an input stream that will be linked to the file specified by&nbsp;<code>fileObject<\/code>.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Methods of FileInputStream<\/h2>\n\n\n\n<p>The&nbsp;<code>FileInputStream<\/code>&nbsp;class provides implementations for different methods present in the&nbsp;<code>InputStream<\/code>&nbsp;class.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"read\">read() Method<\/h3>\n\n\n\n<ul><li><code>read()<\/code>&nbsp;&#8211; reads a single byte from the file<\/li><li><code>read(byte[] array)<\/code>&nbsp;&#8211; reads the bytes from the file and stores in the specified array<\/li><li><code>read(byte[] array, int start, int length)<\/code>&nbsp;&#8211; reads the number of bytes equal to&nbsp;<var>length<\/var>&nbsp;from the file and stores in the specified array starting from the position&nbsp;<var>start<\/var><\/li><\/ul>\n\n\n\n<p>Suppose we have a file named&nbsp;<strong>input.txt<\/strong>&nbsp;with the following content.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>This is a line of text inside the file.\n<\/code><\/pre>\n\n\n\n<p>Let&#8217;s try to read this file using&nbsp;<code>FileInputStream<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.io.FileInputStream;\n\npublic class Main {\n\n  public static void main(String args&#91;]) {\n\n     try {\n        FileInputStream input = new FileInputStream(\"input.txt\");\n\n        System.out.println(\"Data in the file: \");\n\n        \/\/ Reads the first byte\n        int i = input.read();\n\n       while(i != -1) {\n           System.out.print((char)i);\n\n           \/\/ Reads next byte from the file\n           i = input.read();\n        }\n        input.close();\n     }\n\n     catch(Exception e) {\n        e.getStackTrace();\n     }\n  }\n}\n<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>Data in the file:\nThis is a line of text inside the file.\n<\/samp><\/code><\/pre>\n\n\n\n<p>In the above example, we have created a file input stream named&nbsp;<var>input<\/var>. The input stream is linked with the&nbsp;<strong>input.txt<\/strong>&nbsp;file.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>FileInputStream input = new FileInputStream(\"input.txt\");\n<\/code><\/pre>\n\n\n\n<p>To read data from the file, we have used the&nbsp;<code>read()<\/code>&nbsp;method inside the while loop.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"available\">available() Method<\/h3>\n\n\n\n<p>To get the number of available bytes, we can use the&nbsp;<code>available()<\/code>&nbsp;method. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.io.FileInputStream;\n\npublic class Main {\n\n   public static void main(String args&#91;]) {\n\n      try {\n         \/\/ Suppose, the input.txt file contains the following text\n         \/\/ This is a line of text inside the file.\n         FileInputStream input = new FileInputStream(\"input.txt\");\n\n         \/\/ Returns the number of available bytes\n         System.out.println(\"Available bytes at the beginning: \" + input.available());\n\n         \/\/ Reads 3 bytes from the file\n         input.read();\n         input.read();\n         input.read();\n\n         \/\/ Returns the number of available bytes\n         System.out.println(\"Available bytes at the end: \" + input.available());\n\n         input.close();\n      }\n\n      catch (Exception e) {\n         e.getStackTrace();\n      }\n   }\n}\n<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>Available bytes at the beginning: 39\nAvailable bytes at the end: 36\n<\/samp><\/code><\/pre>\n\n\n\n<p>In the above example,<\/p>\n\n\n\n<ol><li>We first use the&nbsp;<code>available()<\/code>&nbsp;method to check the number of available bytes in the file input stream.<\/li><li>We then have used the&nbsp;<code>read()<\/code>&nbsp;method 3 times to read 3 bytes from the file input stream.<\/li><li>Now, after reading the bytes we again have checked the available bytes. This time the available bytes decreased by 3.<\/li><\/ol>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"skip\">skip() Method<\/h3>\n\n\n\n<p>To discard and skip the specified number of bytes, we can use the&nbsp;<code>skip()<\/code>&nbsp;method. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.io.FileInputStream;\n\npublic class Main {\n\n   public static void main(String args&#91;]) {\n\n      try {\n         \/\/ Suppose, the input.txt file contains the following text\n         \/\/ This is a line of text inside the file.\n         FileInputStream input = new FileInputStream(\"input.txt\");\n\n         \/\/ Skips the 5 bytes\n         input.skip(5);\n         System.out.println(\"Input stream after skipping 5 bytes:\");\n\n         \/\/ Reads the first byte\n         int i = input.read();\n         while (i != -1) {\n            System.out.print((char) i);\n\n            \/\/ Reads next byte from the file\n            i = input.read();\n         }\n\n         \/\/ close() method\n         input.close();\n      }\n      catch (Exception e) {\n         e.getStackTrace();\n      }\n   }\n}\n<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>Input Stream after skipping 5 bytes:\nis a line of text inside the file.\n<\/samp><\/code><\/pre>\n\n\n\n<p>In the above example, we have used the&nbsp;<code>skip()<\/code>&nbsp;method to skip 5 bytes of data from the file input stream. Hence, the bytes representing the text&nbsp;<strong>&#8220;This &#8220;<\/strong>&nbsp;is not read from the input stream.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">close() Method<\/h3>\n\n\n\n<p>To close the file input stream, we can use the&nbsp;<code>close()<\/code>&nbsp;method. Once the&nbsp;<code>close()<\/code>&nbsp;method is called, we cannot use the input stream to read data.<\/p>\n\n\n\n<p>In all the above examples, we have used the&nbsp;<code>close()<\/code>&nbsp;method to close the file input stream.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"other-methods\">Other Methods Of FileInputStream<\/h2>\n\n\n\n<figure class=\"wp-block-table is-style-regular\"><table><tbody><tr><th>Methods<\/th><th>Descriptions<\/th><\/tr><tr><td><code>finalize()<\/code><code><\/code><\/td><td>ensures that the <code>close()<\/code> method is called<\/td><\/tr><tr><td><code>getChannel()<\/code><code><\/code><\/td><td>returns the object of <code>FileChannel<\/code> associated with the input stream<\/td><\/tr><tr><td><code>getFD()<\/code><code><\/code><\/td><td>returns the file descriptor associated with the input stream<\/td><\/tr><tr><td><code>mark()<\/code><\/td><td>mark the position in input stream up to which data has been read<\/td><\/tr><tr><td><code>reset()<\/code><\/td><td>returns the control to the point in the input stream where the mark was set<\/td><\/tr><\/tbody><\/table><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we will learn about Java FileInputStream and its methods with the help of examples. The&nbsp;FileInputStream&nbsp;class of the&nbsp;java.io&nbsp;package can be used to read data (in bytes) from files. It extends the&nbsp;InputStream&nbsp;abstract class. Before we learn about\u00a0FileInputStream, make sure to know about\u00a0Java Files. Create a FileInputStream In order to create a file input stream, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[483],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1288"}],"collection":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/comments?post=1288"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1288\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=1288"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=1288"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=1288"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}