{"id":2549,"date":"2020-04-14T23:38:05","date_gmt":"2020-04-14T18:08:05","guid":{"rendered":"https:\/\/www.h2kinfosys.com\/blog\/?p=2549"},"modified":"2020-12-16T14:33:19","modified_gmt":"2020-12-16T09:03:19","slug":"java-input-output-stream","status":"publish","type":"post","link":"https:\/\/www.h2kinfosys.com\/blog\/java-input-output-stream\/","title":{"rendered":"Java Input-Output Stream"},"content":{"rendered":"<h2><b>Introduction to I\/O(Input Output Stream):<\/b><\/h2>\n<ul>\n<li><span style=\"font-weight: 400;\">It<\/span><b><\/b><span style=\"font-weight: 400;\">is used&nbsp;<\/span><i><span style=\"font-weight: 400;\">to process the input<\/span><\/i><span style=\"font-weight: 400;\">&nbsp;and&nbsp;<\/span><i><span style=\"font-weight: 400;\">produce the output<\/span><\/i><span style=\"font-weight: 400;\">.<\/span><\/li>\n<li><span style=\"font-weight: 400;\"> Stream concept is used in Java to make an I\/O operation fast.&nbsp;<\/span><\/li>\n<li><span style=\"font-weight: 400;\"> The java.io package which contains all the classes required for input and output operations.<\/span><\/li>\n<li><span style=\"font-weight: 400;\"> By using Java I\/O API, we can perform&nbsp;<\/span><a href=\"https:\/\/www.h2kinfosys.com\/blog\/reading-and-writing-text-file-java\/\"><b>file handling in Java<\/b><\/a><span style=\"font-weight: 400;\">.<\/span><\/li>\n<\/ul>\n<h2><b>Introduction to Stream:<\/b><\/h2>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">A stream contains a sequence of data.&nbsp;<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">A stream is composed of bytes in <a href=\"https:\/\/www.h2kinfosys.com\/courses\/java-online-training-course-details\">Java<\/a>.&nbsp;<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">It is also called a stream because it is like a stream of water that continues to flow.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">In Java there are three streams are created for us automatically, and all these streams are attached with the console, and here it is:<\/span><\/li>\n<\/ul>\n<ol>\n<li><b>System.out:&nbsp;<\/b><span style=\"font-weight: 400;\">standard output stream<\/span><\/li>\n<li><b>System.in:&nbsp;<\/b><span style=\"font-weight: 400;\">standard input stream<\/span><\/li>\n<li><b>3) System.err:&nbsp;<\/b><span style=\"font-weight: 400;\">standard error stream<\/span><\/li>\n<\/ol>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"wp-image-2550 aligncenter\" src=\"https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/04\/Screenshot_171.png\" alt=\"INPUT-OUTPUT STREAM\" width=\"670\" height=\"224\" title=\"\" srcset=\"https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/04\/Screenshot_171.png 763w, https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/04\/Screenshot_171-300x100.png 300w\" sizes=\"(max-width: 670px) 100vw, 670px\" \/><\/p>\n<p><b>Java is defining two types of streams, and they are:<\/b><\/p>\n<ol>\n<li style=\"font-weight: 400;\"><b>Byte Stream:<\/b><span style=\"font-weight: 400;\">&nbsp;It is used for handling input and output of byte.<\/span><\/li>\n<li style=\"font-weight: 400;\"><b>Character Stream:<\/b>&nbsp;It is used for handling input and output of characters.<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<ol>\n<li>\n<h3><b>Java Byte Stream Classes<\/b><\/h3>\n<\/li>\n<\/ol>\n<p><span style=\"font-weight: 400;\">It is defined by using two <a href=\"https:\/\/www.h2kinfosys.com\/blog\/interfaces-abstract-classes\/\">abstract classes <\/a>at the top of the hierarchy, and they are InputStream(read data from a source) and OutputStream(writing data to a destination).<\/span><\/p>\n<p><img decoding=\"async\" class=\"wp-image-2551 aligncenter\" src=\"https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/04\/Screenshot_172.png\" alt=\"Java Input Output Stream\" width=\"553\" height=\"317\" title=\"\" srcset=\"https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/04\/Screenshot_172.png 647w, https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/04\/Screenshot_172-300x172.png 300w\" sizes=\"(max-width: 553px) 100vw, 553px\" \/><\/p>\n<p><em><strong>Let&#8217;s have an example by using code:<\/strong><\/em><\/p>\n<pre style=\"padding-left: 60px;\">import java.io.*;\n\npublic class ByteStream \n{\n public static void main(String arg[]) throws IOException \n { \n FileInputStream fin = null;\n FileOutputStream fout = null;\n\ntry \n {\n fin = new FileInputStream(\"inputfile.txt\");\n fout = new FileOutputStream(\"outputfile.txt\");\n\nint c;\n while ((c = in.read()) != -1) \n {\n out.write(c);\n }\n }\n finally \n {\n if (in != null) \n {\n in.close();\n }\n if (out != null) \n {\n out.close();\n }\n }\n }\n}<\/pre>\n<p><em><strong>Now let&#8217;s have a file inputfile.txt with the following content <\/strong><\/em><\/p>\n<p>[box type=&#8221;shadow&#8221; align=&#8221;&#8221; class=&#8221;&#8221; width=&#8221;&#8221;]This is a test for copy files in bytestream.[\/box]<\/p>\n<p><span style=\"font-weight: 400;\">Now, compile the above program and execute it, which will result in creating outputfile.txt file with the same content that we have in inputfile.txt. So let&#8217;s put the above code in Bytestream.java file and do the following \u2212<\/span><\/p>\n<p>[box type=&#8221;shadow&#8221; align=&#8221;&#8221; class=&#8221;&#8221; width=&#8221;&#8221;]$javac Bytestream.java $java Bytestream[\/box]<\/p>\n<h3><b>2.Java Character Stream Classes<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">It is also defined by using two abstract classes at the top of the hierarchy, and they are Reader (reading character streams) and Writer (writing to character <a href=\"https:\/\/www.h2kinfosys.com\/blog\/serializable-interface\/\">streams<\/a>).<\/span><\/p>\n<p><img decoding=\"async\" class=\"wp-image-2552 aligncenter\" src=\"https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/04\/Screenshot_173.png\" alt=\"Java Character Stream Classes\" width=\"549\" height=\"396\" title=\"\" srcset=\"https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/04\/Screenshot_173.png 620w, https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/04\/Screenshot_173-300x216.png 300w\" sizes=\"(max-width: 549px) 100vw, 549px\" \/><\/p>\n<p><em><strong>Let&#8217;s have an example by using code:<\/strong><\/em><\/p>\n<pre style=\"padding-left: 60px;\">import java.io. *;\n\npublic class CharacterStream \n{\n public static void main(String arg[]) throws IOException \n {\n FileReader fin = null;\n FileWriter fout = null;\n \n try \n {\n fin = new FileReader(\"inputfile.txt\");\n fout = new FileWriter(\"outputfile.txt\");\n\nint c;\n while ((c = in.read()) != -1) \n {\n\nout.write(c);\n }\n }\n finally\n {\n if (in != null) \n {\n<span style=\"font-weight: 400;\">in.close();<\/span>\n\n<span style=\"font-weight: 400;\">}<\/span>\n\n<span style=\"font-weight: 400;\">if (out != null)&nbsp;<\/span>\n\n<span style=\"font-weight: 400;\">{<\/span>\n\n<span style=\"font-weight: 400;\">out.close();<\/span>\n\n<span style=\"font-weight: 400;\">}<\/span>\n\n<span style=\"font-weight: 400;\">}<\/span>\n\n<span style=\"font-weight: 400;\">}<\/span>\n\n<span style=\"font-weight: 400;\">}&nbsp; &nbsp; <\/span>\n\n\n\n<\/pre>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400;\">Now let&#8217;s have a file&nbsp;<\/span><b>inputfile.txt<\/b><span style=\"font-weight: 400;\">&nbsp;with the following content \u2212<\/span><\/p>\n<p>[box type=&#8221;shadow&#8221; align=&#8221;&#8221; class=&#8221;&#8221; width=&#8221;&#8221;]This is test for copy file for Character stream.[\/box]<\/p>\n<p><span style=\"font-weight: 400;\">Now, compile the above program and execute it, which will result in creating outputfile.txt file with the same content that we have in inputfile.txt. So let&#8217;s put the above code in CharacterStream.java file and do the following \u2013<\/span><\/p>\n<p>[box type=&#8221;shadow&#8221; align=&#8221;&#8221; class=&#8221;&#8221; width=&#8221;&#8221;]$javac CharacterStream.java $java CharacterStream[\/box]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction to I\/O(Input Output Stream): Itis used&nbsp;to process the input&nbsp;and&nbsp;produce the output. Stream concept is used in Java to make an I\/O operation fast.&nbsp; The java.io package which contains all the classes required for input and output operations. By using Java I\/O API, we can perform&nbsp;file handling in Java. Introduction to Stream: A stream contains [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2579,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[42],"tags":[521,519,58,520],"class_list":["post-2549","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java-tutorials","tag-file-handling","tag-input-output","tag-java","tag-stream-classes"],"_links":{"self":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/2549","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/comments?post=2549"}],"version-history":[{"count":0,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/2549\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media\/2579"}],"wp:attachment":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media?parent=2549"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/categories?post=2549"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/tags?post=2549"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}