{"id":105,"date":"2025-02-21T09:47:31","date_gmt":"2025-02-21T09:47:31","guid":{"rendered":"https:\/\/alpeshconnect.in\/blog\/?p=105"},"modified":"2025-02-21T09:47:31","modified_gmt":"2025-02-21T09:47:31","slug":"explain-inputstream-class-outputstream-class-reader-class-and-writer-class","status":"publish","type":"post","link":"https:\/\/alpeshconnect.in\/blog\/2025\/02\/21\/explain-inputstream-class-outputstream-class-reader-class-and-writer-class\/","title":{"rendered":"Explain InputStream class, OutputStream class, Reader class and Writer class"},"content":{"rendered":"\n<p>Explain InputStream class, OutputStream class, Reader class and Writer class with<br>suitable java code.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong>InputStream Class<\/strong><\/h3>\n\n\n\n<p>The <strong>InputStream<\/strong> class is an abstract class that represents input byte streams. It is the superclass of all byte input stream classes. The methods in this class are used to read bytes from an underlying input source.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Key Methods:<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>int read()<\/code>: Reads the next byte of data from the input stream.<\/li>\n\n\n\n<li><code>int read(byte[] b)<\/code>: Reads up to <code>b.length<\/code> bytes of data into an array.<\/li>\n\n\n\n<li><code>void close()<\/code>: Closes the input stream and releases any system resources associated with it.<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">Example:<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.io.*;\n\npublic class InputStreamExample {\n    public static void main(String&#91;] args) throws IOException {\n        FileInputStream inputStream = new FileInputStream(\"example.txt\");\n        int data;\n        while ((data = inputStream.read()) != -1) {\n            System.out.print((char) data);\n        }\n        inputStream.close();\n    }\n}\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2. <strong>OutputStream Class<\/strong><\/h3>\n\n\n\n<p>The <strong>OutputStream<\/strong> class is an abstract class that represents output byte streams. It is the superclass of all byte output stream classes. The methods in this class are used to write bytes to an output destination, like a file or network.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Key Methods:<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>void write(int b)<\/code>: Writes the specified byte to the output stream.<\/li>\n\n\n\n<li><code>void write(byte[] b)<\/code>: Writes <code>b.length<\/code> bytes from the specified byte array to the output stream.<\/li>\n\n\n\n<li><code>void close()<\/code>: Closes the output stream and releases any system resources associated with it.<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">Example:<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.io.*;\n\npublic class OutputStreamExample {\n    public static void main(String&#91;] args) throws IOException {\n        FileOutputStream outputStream = new FileOutputStream(\"output.txt\");\n        String text = \"Hello, World!\";\n        outputStream.write(text.getBytes());\n        outputStream.close();\n    }\n}\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3. <strong>Reader Class<\/strong><\/h3>\n\n\n\n<p>The <strong>Reader<\/strong> class is an abstract class that represents input character streams. It is used to read characters, arrays, and lines of text. Unlike <strong>InputStream<\/strong>, which deals with bytes, <strong>Reader<\/strong> works with character data.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Key Methods:<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>int read()<\/code>: Reads a single character from the stream.<\/li>\n\n\n\n<li><code>int read(char[] c)<\/code>: Reads characters into an array.<\/li>\n\n\n\n<li><code>void close()<\/code>: Closes the stream and releases any system resources associated with it.<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">Example:<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.io.*;\n\npublic class ReaderExample {\n    public static void main(String&#91;] args) throws IOException {\n        FileReader reader = new FileReader(\"example.txt\");\n        int data;\n        while ((data = reader.read()) != -1) {\n            System.out.print((char) data);\n        }\n        reader.close();\n    }\n}\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">4. <strong>Writer Class<\/strong><\/h3>\n\n\n\n<p>The <strong>Writer<\/strong> class is an abstract class that represents output character streams. It is used to write characters, arrays, and lines of text to an output destination.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Key Methods:<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>void write(int c)<\/code>: Writes a single character to the stream.<\/li>\n\n\n\n<li><code>void write(char[] c)<\/code>: Writes characters from an array to the stream.<\/li>\n\n\n\n<li><code>void close()<\/code>: Closes the stream and releases any system resources associated with it.<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">Example:<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.io.*;\n\npublic class WriterExample {\n    public static void main(String&#91;] args) throws IOException {\n        FileWriter writer = new FileWriter(\"output.txt\");\n        String text = \"Hello, Writer!\";\n        writer.write(text);\n        writer.close();\n    }\n}\n<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Explain InputStream class, OutputStream class, Reader class and Writer class withsuitable java code. 1. InputStream Class The InputStream class is an abstract class that represents input byte streams. It is the superclass of all byte input stream classes. The methods in this class are used to read bytes from an underlying input source. Key Methods: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":97,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,28],"tags":[31,29,30],"class_list":["post-105","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-mca","category-object-oriented-programming-in-java","tag-mca","tag-mca-paper-solution","tag-object-oriented-programming-in-java"],"_links":{"self":[{"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/posts\/105","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/comments?post=105"}],"version-history":[{"count":1,"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/posts\/105\/revisions"}],"predecessor-version":[{"id":106,"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/posts\/105\/revisions\/106"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/media\/97"}],"wp:attachment":[{"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/media?parent=105"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/categories?post=105"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/tags?post=105"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}