{"id":89857,"date":"2024-05-20T18:00:24","date_gmt":"2024-05-20T12:30:24","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=89857"},"modified":"2024-05-20T18:03:27","modified_gmt":"2024-05-20T12:33:27","slug":"java-pushbackreader-class","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/java-pushbackreader-class\/","title":{"rendered":"Java PushbackReader Class with Examples"},"content":{"rendered":"<p>Java is wide range and used in various applications.<\/p>\n<p>PushbackReader is discussed in a detailed manner. It allows characters to be pushed back into the Stream. The unread() method of this class allows us to pushback the characters.<\/p>\n<h2>Explanation of Java PushbackReader Class:<\/h2>\n<ul>\n<li>A character stream reader is the Java PushbackReader class.<\/li>\n<li>It overrides the FilterReader class and pushes a character back into the stream.<\/li>\n<li>Pushing characters back into the stream is possible with this class, which is a character stream class.<\/li>\n<li>The characters can be pushed back using this class&#8217;s unread() function.<\/li>\n<\/ul>\n<h3>Pushback explanation:<\/h3>\n<ul>\n<li>A byte can be received from an input stream and then &#8220;pushed back&#8221;\u2014that is, returned to the stream\u2014by using pushback.<\/li>\n<li>The PushbackInputStream class puts this concept into practice. It offers a way to &#8220;peek&#8221; into data coming from an input stream without interfering with it.<\/li>\n<\/ul>\n<h3>How to create Java PushbackReader:<\/h3>\n<ul>\n<li>You must first build an instance of a Java PushbackReader before you can utilize it.<\/li>\n<li>By utilizing the new operator in ordinary object instantiation, you can create a PushbackReader.<\/li>\n<li>A Java Reader is required to be passed to the PushbackReader constructor in order for it to read the characters.<\/li>\n<\/ul>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">PushbackReader pushbackReader =\r\n    new PushbackReader(new FileReader(\" \"))<\/pre>\n<h3>Declaration:<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public class PushbackReader extends FilterReader<\/pre>\n<h4>Parameter:<\/h4>\n<ul>\n<li>C is the int value of the character that has to be pushed back.<\/li>\n<\/ul>\n<p><strong>Return Value:<\/strong><br \/>\nIt does not return any Value<\/p>\n<h4>Exception:<\/h4>\n<ul>\n<li>IOException \u2212 When an I\/O error occurs or the pushback buffer fills up.<\/li>\n<\/ul>\n<h4>Read characters:<\/h4>\n<p>It used to read the characters of pushbackreader:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">int aChar = pushbackReader.read();\r\nwhile(aChar != -1) {\r\n    System.out.println((char) aChar);\r\n    aChar = pushbackReader.read();\r\n}<\/pre>\n<h3>Class Methods:<\/h3>\n<table>\n<tbody>\n<tr>\n<td><strong>Method<\/strong><\/td>\n<td><strong>Description<\/strong><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">int read()<\/span><\/td>\n<td><span style=\"font-weight: 400;\">It reads one character at a time.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">void mark(int readAheadLimit)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">It serves as a marker for the current location within a stream.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">boolean ready()<\/span><\/td>\n<td><span style=\"font-weight: 400;\">It is employed to determine when the stream is prepared for reading.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">boolean markSupported()<\/span><\/td>\n<td><span style=\"font-weight: 400;\">To find out if the stream supports the mark() action, use this.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">long skip(long n)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">The character is skipped using it.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">void unread (int c)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">By copying the character to the pushback buffer, it is utilized to push the character back.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">void unread (char[] cbuf)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">It is used to push the character back by copying it to the pushback buffer.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">void reset()<\/span><\/td>\n<td><span style=\"font-weight: 400;\">The stream is reset using it.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">void close()<\/span><\/td>\n<td><span style=\"font-weight: 400;\">The stream is closed using it.<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>Constructor:<\/h3>\n<table>\n<tbody>\n<tr>\n<td><strong>Constructor<\/strong><\/td>\n<td><strong>Description<\/strong><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">PushbackReader(Reader in )<\/span><\/td>\n<td><span style=\"font-weight: 400;\">It creates a new pushback with one character pushback buffer.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">PushbackReader (Reader in, int size)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">It creates a new pushback with pushback with given size.<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>Example 1:<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import java.io.*;\r\n\r\npublic class TechVidvan {\r\n    public static void main(String[] args) throws Exception {\r\n        char ary[] = {'1', '-', '-', '2', '-', '3', '4', '-', '5'};\r\n        CharArrayReader reader = new CharArrayReader(ary);\r\n        PushbackReader push = new PushbackReader(reader);\r\n\r\n        int i;\r\n        while ((i = push.read()) != -1) {\r\n            if (i == '-') {\r\n                int j;\r\n                if ((j = push.read()) == '-') {\r\n                    System.out.print(\"#*\");\r\n                } else {\r\n                    push.unread(j); \/\/ push back single character\r\n                    System.out.print((char) i);\r\n                }\r\n            } else {\r\n                System.out.print((char) i);\r\n            }\r\n        }\r\n    }\r\n}<\/pre>\n<p><strong>Output:<\/strong><br \/>\n1#*2-345<\/p>\n<h4>Example 2:<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import java.io.*;\r\n\r\npublic class TechVidvan{\r\n   public static void main(String[] args) {\r\n      String s = \"TechVidvan\";\r\n\r\n            StringReader sr = new StringReader(s);\r\n\r\n            PushbackReader pr = new PushbackReader(sr, 20);\r\n\r\n      try {\r\n                  for (int i = 0; i &lt; 5; i++) {\r\n            char c = (char) pr.read();\r\n            System.out.print(\"\" + c);\r\n         }\r\n\r\n                  System.out.println();\r\n\r\n                  pr.unread('A');\r\n\r\n                  char c = (char) pr.read();\r\n\r\n                  System.out.println(\"\" + c);\r\n\r\n                  pr.close();\r\n\r\n      } catch (IOException ex) {\r\n         ex.printStackTrace();\r\n      }\r\n   }\r\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p>TechV<br \/>\nA<\/p>\n<h4>Example 3:<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import java.io.PushbackReader;\r\nimport java.io.StringReader;\r\npublic class TechVidvan\r\n{\r\n    public static void main(String args[])\r\n    {\r\n        try\r\n        {  \r\n            String str = \u201cWelcome to TechVidvan Java Tutorials\u201d; \r\n            StringReader reader = new StringReader(str); \r\n            PushbackReader pushbackReader = new PushbackReader(reader); \r\n            System.out.println(\"pushbackReader ready: \" + pushbackReader.ready()); \r\n            for (int i = 0; i &lt; 9; i++) \t\r\n            { \r\n                char ch = (char) pushbackReader.read(); \r\n                System.out.print(ch); \r\n                pushbackReader.skip(1); \r\n            } \r\n        }\r\n        catch (Exception e)\t{  \r\n            System.out.print(\"Error: \"+e.toString());\r\n        }  \r\n    }\r\n}<\/pre>\n<p><strong>Output:<\/strong><br \/>\npushbackReader ready: true<br \/>\nWloet ehi<\/p>\n<h3>Constructors:<\/h3>\n<ul>\n<li><strong>PushbackReader(Reader push):<\/strong> Makes a new Pushback Reader and uses a character Pushback buffer to &#8220;push&#8221; it.<\/li>\n<li><strong>PushbackReader(Reader push, int size):<\/strong> Specifies the size of the pushback buffer when creating a new Pushback Reader.<\/li>\n<\/ul>\n<h3>Benefits:<\/h3>\n<ul>\n<li>One or more characters can be returned to the input stream using the PushbackReader class. You can now see into the input stream thanks to this.<\/li>\n<li>generates a buffered stream that permits the pushing back of a single character. bufSize is the size of the pushback buffer that is passed.<\/li>\n<\/ul>\n<h3>Closing of PushBackReader:<\/h3>\n<ul>\n<li>It is important to remember to close the PushbackReader when you have completed reading the characters.<\/li>\n<li>The Reader instance that a PushbackReader is reading from will also close when the PushbackReader is closed.<\/li>\n<li><strong>By using this to close: <\/strong>pushbackReader.close()<\/li>\n<\/ul>\n<h3>Conclusion<\/h3>\n<p>We have learned about the PushbackReader details. It is used as a pushbackreader in the stream. This feature is handy for parsing scenarios where you may need to backtrack in the input stream.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java is wide range and used in various applications. PushbackReader is discussed in a detailed manner. It allows characters to be pushed back into the Stream. The unread() method of this class allows us&#46;&#46;&#46;<\/p>\n","protected":false},"author":6,"featured_media":447251,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[183],"tags":[296,351,352,299,263,250,353,5528,354,355,5646],"class_list":["post-89857","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","tag-java","tag-java-pushbackreader","tag-java-pushbackreader-class","tag-java-tutorial","tag-java-tutorial-for-beginners","tag-learn-java","tag-pushbackreader","tag-pushbackreader-class","tag-pushbackreader-class-in-java","tag-pushbackreader-in-java","tag-pushbackreader-in-java-with-examples"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Java PushbackReader Class with Examples - TechVidvan<\/title>\n<meta name=\"description\" content=\"Java PushbackReader is discussed in a detailed manner. It allows characters to be pushed back into the Stream.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/java-pushbackreader-class\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java PushbackReader Class with Examples - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Java PushbackReader is discussed in a detailed manner. It allows characters to be pushed back into the Stream.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/java-pushbackreader-class\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2024-05-20T12:30:24+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-05-20T12:33:27+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/08\/java-pushbackreader.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Java PushbackReader Class with Examples - TechVidvan","description":"Java PushbackReader is discussed in a detailed manner. It allows characters to be pushed back into the Stream.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/techvidvan.com\/tutorials\/java-pushbackreader-class\/","og_locale":"en_US","og_type":"article","og_title":"Java PushbackReader Class with Examples - TechVidvan","og_description":"Java PushbackReader is discussed in a detailed manner. It allows characters to be pushed back into the Stream.","og_url":"https:\/\/techvidvan.com\/tutorials\/java-pushbackreader-class\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2024-05-20T12:30:24+00:00","article_modified_time":"2024-05-20T12:33:27+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/08\/java-pushbackreader.webp","type":"image\/webp"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/java-pushbackreader-class\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-pushbackreader-class\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/dde481bb412350cde1ed6e389bc0deaf"},"headline":"Java PushbackReader Class with Examples","datePublished":"2024-05-20T12:30:24+00:00","dateModified":"2024-05-20T12:33:27+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-pushbackreader-class\/"},"wordCount":581,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-pushbackreader-class\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/08\/java-pushbackreader.webp","keywords":["java","java pushbackreader","java pushbackreader class","Java Tutorial","java tutorial for beginners","Learn Java","pushbackreader","pushbackReader class","pushbackreader class in java","pushbackreader in java","pushbackreader in java with examples"],"articleSection":["Java Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/java-pushbackreader-class\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/java-pushbackreader-class\/","url":"https:\/\/techvidvan.com\/tutorials\/java-pushbackreader-class\/","name":"Java PushbackReader Class with Examples - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-pushbackreader-class\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-pushbackreader-class\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/08\/java-pushbackreader.webp","datePublished":"2024-05-20T12:30:24+00:00","dateModified":"2024-05-20T12:33:27+00:00","description":"Java PushbackReader is discussed in a detailed manner. It allows characters to be pushed back into the Stream.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-pushbackreader-class\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/java-pushbackreader-class\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/java-pushbackreader-class\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/08\/java-pushbackreader.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/08\/java-pushbackreader.webp","width":1200,"height":628,"caption":"java pushbackreader"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/java-pushbackreader-class\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Java PushbackReader Class with Examples"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/dde481bb412350cde1ed6e389bc0deaf","name":"TechVidvan Team"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/89857","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=89857"}],"version-history":[{"count":3,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/89857\/revisions"}],"predecessor-version":[{"id":447502,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/89857\/revisions\/447502"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/447251"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=89857"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=89857"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=89857"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}