{"id":1298,"date":"2022-02-27T06:06:38","date_gmt":"2022-02-27T06:06:38","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=1298"},"modified":"2022-02-27T06:06:38","modified_gmt":"2022-02-27T06:06:38","slug":"input-stream-reader-class","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/02\/27\/input-stream-reader-class\/","title":{"rendered":"Input Stream Reader Class"},"content":{"rendered":"\n<p>In this tutorial, we will learn about Java InputStreamReader and its methods with the help of examples.<\/p>\n\n\n\n<p id=\"introduction\">The&nbsp;<code>InputStreamReader<\/code>&nbsp;class of the&nbsp;<code>java.io<\/code>&nbsp;package can be used to convert data in bytes into data in characters.<\/p>\n\n\n\n<p>It extends the abstract class&nbsp;<code>Reader<\/code>.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.programiz.com\/sites\/tutorial2program\/files\/java-%20inputstreamreader.png\" alt=\"The InputStreamReader class is a suclass of Java Reader.\" title=\"Java InputStreamReader Class\"\/><\/figure>\n\n\n\n<p>The&nbsp;<code>InputStreamReader<\/code>&nbsp;class works with other input streams. It is also known as a bridge between byte streams and character streams. This is because the&nbsp;<code>InputStreamReader<\/code>&nbsp;reads bytes from the input stream as characters.<\/p>\n\n\n\n<p>For example, some characters required 2 bytes to be stored in the storage. To read such data we can use the input stream reader that reads the 2 bytes together and converts into the corresponding character.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"create\">Create an InputStreamReader<\/h2>\n\n\n\n<p>In order to create an&nbsp;<code>InputStreamReader<\/code>, we must import the&nbsp;<code>java.io.InputStreamReader<\/code>&nbsp;package first. Once we import the package here is how we can create the input stream reader.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Creates an InputStream\nFileInputStream file = new FileInputStream(String path);\n\n\/\/ Creates an InputStreamReader\nInputStreamReader input = new InputStreamReader(file);\n<\/code><\/pre>\n\n\n\n<p>In the above example, we have created an&nbsp;<code>InputStreamReader<\/code>&nbsp;named&nbsp;<var>input<\/var>&nbsp;along with the&nbsp;<code>FileInputStream<\/code>&nbsp;named&nbsp;<var>file<\/var>.<\/p>\n\n\n\n<p>Here, the data in the file are stored using some default character encoding.<\/p>\n\n\n\n<p>However, we can specify the type of character encoding (<strong>UTF8<\/strong>&nbsp;or&nbsp;<strong>UTF16<\/strong>) in the file as well.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Creates an InputStreamReader\n specifying the character encoding\nInputStreamReader input = new InputStreamReader(file, Charset cs);\n<\/code><\/pre>\n\n\n\n<p>Here, we have used the&nbsp;<code>Charset<\/code>&nbsp;class to specify the character encoding in the file.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Methods of InputStreamReader<\/h2>\n\n\n\n<p>The&nbsp;<code>InputStreamReader<\/code>&nbsp;class provides implementations for different methods present in the&nbsp;<code>Reader<\/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 character from the reader<\/li><li><code>read(char[] array)<\/code>&nbsp;&#8211; reads the characters from the reader and stores in the specified array<\/li><li><code>read(char[] array, int start, int length)<\/code>&nbsp;&#8211; reads the number of characters equal to&nbsp;<var>length<\/var>&nbsp;from the reader and stores in the specified array starting from the&nbsp;<var>start<\/var><\/li><\/ul>\n\n\n\n<p>For example, 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>InputStreamReader<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.io.InputStreamReader;\nimport java.io.FileInputStream;\n\nclass Main {\n  public static void main(String&#91;] args) {\n\n    \/\/ Creates an array of character\n    char&#91;] array = new char&#91;100];\n\n    try {\n      \/\/ Creates a FileInputStream\n      FileInputStream file = new FileInputStream(\"input.txt\");\n\n      \/\/ Creates an InputStreamReader\n      InputStreamReader input = new InputStreamReader(file);\n\n      \/\/ Reads characters from the file\n      input.read(array);\n      System.out.println(\"Data in the stream:\");\n      System.out.println(array);\n\n      \/\/ Closes the reader\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 stream:\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 an input stream reader using the file input stream. The input stream reader is linked with the file&nbsp;<strong>input.txt<\/strong>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> FileInputStream file = new FileInputStream(\"input.txt\");\n\n InputStreamReader input = new InputStreamReader(file);\n\n<\/code><\/pre>\n\n\n\n<p>To read characters from the file, we have used the&nbsp;<code>read()<\/code>&nbsp;method.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"getEncoding\">getEncoding() Method<\/h2>\n\n\n\n<p>The&nbsp;<code>getEncoding()<\/code>&nbsp;method can be used to get the type of encoding that is used to store data in the input stream. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.io.InputStreamReader;\nimport java.nio.charset.Charset;\nimport java.io.FileInputStream;\n\nclass Main {\n  public static void main(String&#91;] args) {\n\n    try {\n      \/\/ Creates a FileInputStream\n      FileInputStream file = new FileInputStream(\"input.txt\");\n\n      \/\/ Creates an InputStreamReader with default encoding\n      InputStreamReader input1 = new InputStreamReader(file);\n\n      \/\/ Creates an InputStreamReader specifying the encoding\n      InputStreamReader input2 = new InputStreamReader(file, Charset.forName(\"UTF8\"));\n\n      \/\/ Returns the character encoding of the input stream\n      System.out.println(\"Character encoding of input1: \" + input1.getEncoding());\n      System.out.println(\"Character encoding of input2: \" + input2.getEncoding());\n\n      \/\/ Closes the reader\n      input1.close();\n      input2.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>The character encoding of input1: Cp1252\nThe character encoding of input2: UTF8\n<\/samp><\/code><\/pre>\n\n\n\n<p>In the above example, we have created 2 input stream reader named&nbsp;<var>input1<\/var>&nbsp;and&nbsp;<var>input2<\/var>.<\/p>\n\n\n\n<ul><li><var>input1<\/var>&nbsp;does not specify the character encoding. Hence the&nbsp;<code>getEncoding()<\/code>&nbsp;method returns the canonical name of the default character encoding.<\/li><li><var>input2<\/var>&nbsp;specifies the character encoding,&nbsp;<strong>UTF8<\/strong>. Hence the&nbsp;<code>getEncoding()<\/code>&nbsp;method returns the specified character encoding.<\/li><\/ul>\n\n\n\n<p><strong>Note<\/strong>: We have used the\u00a0<code>Charset.forName()<\/code>\u00a0method to specify the type of character encoding.<\/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 input stream reader, we can use the&nbsp;<code>close()<\/code>&nbsp;method. Once the&nbsp;<code>close()<\/code>&nbsp;method is called, we cannot use the reader to read the data.<\/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 InputStreamReader<\/h2>\n\n\n\n<figure class=\"wp-block-table is-style-regular\"><table><tbody><tr><th>Method<\/th><th>Description<\/th><\/tr><tr><td><code>ready()<\/code><\/td><td>checks if the stream is ready to be read<\/td><\/tr><tr><td><code>mark()<\/code><\/td><td>mark the position in 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 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 InputStreamReader and its methods with the help of examples. The&nbsp;InputStreamReader&nbsp;class of the&nbsp;java.io&nbsp;package can be used to convert data in bytes into data in characters. It extends the abstract class&nbsp;Reader. The&nbsp;InputStreamReader&nbsp;class works with other input streams. It is also known as a bridge between byte streams and character [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[532],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1298"}],"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=1298"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1298\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=1298"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=1298"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=1298"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}