{"id":1284,"date":"2022-02-26T07:44:39","date_gmt":"2022-02-26T07:44:39","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=1284"},"modified":"2022-02-26T07:44:39","modified_gmt":"2022-02-26T07:44:39","slug":"input-stream-class","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/02\/26\/input-stream-class\/","title":{"rendered":"Input Stream Class"},"content":{"rendered":"\n<p>In this tutorial, we will learn about the Java InputStream class and its methods with the help of an example.<\/p>\n\n\n\n<p id=\"introduction\">The&nbsp;<code>InputStream<\/code>&nbsp;class of the&nbsp;<code>java.io<\/code>&nbsp;package is an abstract superclass that represents an input stream of bytes.<\/p>\n\n\n\n<p>Since&nbsp;<code>InputStream<\/code>&nbsp;is an abstract class, it is not useful by itself. However, its subclasses can be used to read data.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"subclasses\">Subclasses of InputStream<\/h2>\n\n\n\n<p>In order to use the functionality of&nbsp;<code>InputStream<\/code>, we can use its subclasses. Some of them are:<\/p>\n\n\n\n<ul><li>FileInputStream<\/li><li>ByteArrayInputStream<\/li><li>ObjectInputStream<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.programiz.com\/sites\/tutorial2program\/files\/java%20inputstream-subclasses.png\" alt=\"Java InputStream subclasses are FileInputStream, ByteArrayInputStream and ObjectInputStream.\" title=\"Java InputStream Class\"\/><\/figure>\n\n\n\n<p>We will learn about all these subclasses in the next tutorial.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"create\">Create an InputStream<\/h2>\n\n\n\n<p>In order to create an InputStream, we must import the&nbsp;<code>java.io.InputStream<\/code>&nbsp;package first. Once we import the package, here is how we can create the input stream.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Creates an InputStream\nInputStream object1 = new FileInputStream();\n<\/code><\/pre>\n\n\n\n<p>Here, we have created an input stream using&nbsp;<code>FileInputStream<\/code>. It is because&nbsp;<code>InputStream<\/code>&nbsp;is an abstract class. Hence we cannot create an object of&nbsp;<code>InputStream<\/code>.<\/p>\n\n\n\n<p><strong>Note<\/strong>: We can also create an input stream from other subclasses of&nbsp;<code>InputStream<\/code>.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"methods\">Methods of InputStream<\/h2>\n\n\n\n<p>The&nbsp;<code>InputStream<\/code>&nbsp;class provides different methods that are implemented by its subclasses. Here are some of the commonly used methods:<\/p>\n\n\n\n<ul><li><code>read()<\/code>&nbsp;&#8211; reads one byte of data from the input stream<\/li><li><code>read(byte[] array)<\/code>&nbsp;&#8211; reads bytes from the stream and stores in the specified array<\/li><li><code>available()<\/code>&nbsp;&#8211; returns the number of bytes available in the input stream<\/li><li><code>mark()<\/code>&nbsp;&#8211; marks the position in the input stream up to which data has been read<\/li><li><code>reset()<\/code>&nbsp;&#8211; returns the control to the point in the stream where the mark was set<\/li><li><code>markSupported()<\/code>&nbsp;&#8211; checks if the&nbsp;<code>mark()<\/code>&nbsp;and&nbsp;<code>reset()<\/code>&nbsp;method is supported in the stream<\/li><li><code>skips()<\/code>&nbsp;&#8211; skips and discards the specified number of bytes from the input stream<\/li><li><code>close()<\/code>&nbsp;&#8211; closes the input stream<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"example\">Example: InputStream Using FileInputStream<\/h2>\n\n\n\n<p>Here is how we can implement&nbsp;<code>InputStream<\/code>&nbsp;using the&nbsp;<code>FileInputStream<\/code>&nbsp;class.<\/p>\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>&nbsp;(a subclass of&nbsp;<code>InputStream<\/code>).<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.io.FileInputStream;\nimport java.io.InputStream;\n\nclass Main {\n  public static void main(String args&#91;]) {\n\n    byte&#91;] array = new byte&#91;100];\n\n    try {\n      InputStream input = new FileInputStream(\"input.txt\");\n\n      System.out.println(\"Available bytes in the file: \" + input.available());\n\n      \/\/ Read byte from the input stream\n      input.read(array);\n      System.out.println(\"Data read from the file: \");\n\n      \/\/ Convert byte array into string\n      String data = new String(array);\n      System.out.println(data);\n\n      \/\/ Close the input stream\n      input.close();\n    } catch (Exception e) {\n      e.getStackTrace();\n    }\n  }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Available bytes in the file: 39\nData read from the file:\nThis is a line of text inside the file\n<\/code><\/pre>\n\n\n\n<p>In the above example, we have created an input stream using the&nbsp;<code>FileInputStream<\/code>&nbsp;class. The input stream is linked with the file&nbsp;<strong>input.txt<\/strong>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>InputStream input = new FileInputStream(\"input.txt\");\n<\/code><\/pre>\n\n\n\n<p>To read data from the&nbsp;<strong>input.txt<\/strong>&nbsp;file, we have implemented these two methods.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>input.read(array); \n \/\/ to read data from the input stream\ninput.close();        \n    \/\/ to close the input stream\n\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we will learn about the Java InputStream class and its methods with the help of an example. The&nbsp;InputStream&nbsp;class of the&nbsp;java.io&nbsp;package is an abstract superclass that represents an input stream of bytes. Since&nbsp;InputStream&nbsp;is an abstract class, it is not useful by itself. However, its subclasses can be used to read data. Subclasses of [&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\/1284"}],"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=1284"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1284\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=1284"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=1284"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=1284"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}