{"id":1294,"date":"2022-02-27T06:03:05","date_gmt":"2022-02-27T06:03:05","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=1294"},"modified":"2022-02-27T06:03:05","modified_gmt":"2022-02-27T06:03:05","slug":"reader-class","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/02\/27\/reader-class\/","title":{"rendered":"Reader Class"},"content":{"rendered":"\n<p>In this tutorial, we will learn about Java Reader, its subclasses and its methods with the help of an example.<\/p>\n\n\n\n<p id=\"introduction\">The&nbsp;<code>Reader<\/code>&nbsp;class of the&nbsp;<code>java.io<\/code>&nbsp;package is an abstract superclass that represents a stream of characters.<\/p>\n\n\n\n<p>Since&nbsp;<code>Reader<\/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 Reader<\/h2>\n\n\n\n<p>In order to use the functionality of&nbsp;<code>Reader<\/code>, we can use its subclasses. Some of them are:<\/p>\n\n\n\n<ul><li>BufferedReader<\/li><li>InputStreamReader<\/li><li>FileReader<\/li><li>StringReader<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.programiz.com\/sites\/tutorial2program\/files\/java-reader-class.png\" alt=\"Subclasses of Java Reader are BufferedReader, InputStreamReader, FileReader and StringReader.\" title=\"Java Reader 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 a Reader<\/h2>\n\n\n\n<p>In order to create a&nbsp;<code>Reader<\/code>, we must import the&nbsp;<code>java.io.Reader<\/code>&nbsp;package first. Once we import the package, here is how we can create the reader.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Creates a Reader\nReader input = new FileReader();\n<\/code><\/pre>\n\n\n\n<p>Here, we have created a reader using the&nbsp;<code>FileReader<\/code>&nbsp;class. It is because&nbsp;<code>Reader<\/code>&nbsp;is an abstract class. Hence we cannot create an object of&nbsp;<code>Reader<\/code>.<\/p>\n\n\n\n<p><strong>Note<\/strong>: We can also create readers from other subclasses of&nbsp;<code>Reader<\/code>.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"methods\">Methods of Reader<\/h2>\n\n\n\n<p>The&nbsp;<code>Reader<\/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>ready()<\/code>&nbsp;&#8211; checks if the reader is ready to be read<\/li><li><code>read(char[] array)<\/code>&nbsp;&#8211; reads the characters from the stream 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 stream and stores in the specified array starting from the&nbsp;<var>start<\/var><\/li><li><code>mark()<\/code>&nbsp;&#8211; marks the position in the 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 is set<\/li><li><code>skip()<\/code>&nbsp;&#8211; discards the specified number of characters from the stream<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"example\">Example: Reader Using FileReader<\/h2>\n\n\n\n<p>Here is how we can implement&nbsp;<code>Reader<\/code>&nbsp;using the&nbsp;<code>FileReader<\/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>FileReader<\/code>&nbsp;(a subclass of&nbsp;<code>Reader<\/code>).<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.io.Reader;\nimport java.io.FileReader;\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 reader using the FileReader\n            Reader input = new FileReader(\"input.txt\");\n\n            \/\/ Checks if reader is ready \n            System.out.println(\"Is there data in the stream?  \" + input.ready());\n\n            \/\/ Reads characters\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>Is there data in the stream?  true\nData 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 a reader using the&nbsp;<code>FileReader<\/code>&nbsp;class. The reader is linked with the file&nbsp;<strong>input.txt<\/strong>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Reader input = new FileReader(\"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 methods.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>input.read();\n       \/\/ to read data from the reader\ninput.close(); \n     \/\/ to close the reader<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we will learn about Java Reader, its subclasses and its methods with the help of an example. The&nbsp;Reader&nbsp;class of the&nbsp;java.io&nbsp;package is an abstract superclass that represents a stream of characters. Since&nbsp;Reader&nbsp;is an abstract class, it is not useful by itself. However, its subclasses can be used to read data. Subclasses of Reader [&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\/1294"}],"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=1294"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1294\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=1294"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=1294"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=1294"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}