{"id":1286,"date":"2022-02-26T07:46:50","date_gmt":"2022-02-26T07:46:50","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=1286"},"modified":"2022-02-26T07:46:50","modified_gmt":"2022-02-26T07:46:50","slug":"output-stream-class","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/02\/26\/output-stream-class\/","title":{"rendered":"Output Stream Class"},"content":{"rendered":"\n<p>In this tutorial, we will learn about the Java OutputStream and its methods with the help of an example.<\/p>\n\n\n\n<p id=\"introduction\">The&nbsp;<code>OutputStream<\/code>&nbsp;class of the&nbsp;<code>java.io<\/code>&nbsp;package is an abstract superclass that represents an output stream of bytes.<\/p>\n\n\n\n<p>Since&nbsp;<code>OutputStream<\/code>&nbsp;is an abstract class, it is not useful by itself. However, its subclasses can be used to write data.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"subclasses\">Subclasses of OutputStream<\/h2>\n\n\n\n<p>In order to use the functionality of&nbsp;<code>OutputStream<\/code>, we can use its subclasses. Some of them are:<\/p>\n\n\n\n<ul><li>FileOutputStream<\/li><li>ByteArrayOutputStream<\/li><li>ObjectOutputStream<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.programiz.com\/sites\/tutorial2program\/files\/java%20outputstream-subclasses.png\" alt=\"Subclasses of Java OutputStream are FileOutputStream, ByteArrayOutputStream and ObjectOutputStream.\" title=\"Java OutputStream \"\/><\/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 an OutputStream<\/h2>\n\n\n\n<p>In order to create an&nbsp;<code>OutputStream<\/code>, we must import the&nbsp;<code>java.io.OutputStream<\/code>&nbsp;package first. Once we import the package, here is how we can create the output stream.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Creates an OutputStream\nOutputStream object = new FileOutputStream();\n<\/code><\/pre>\n\n\n\n<p>Here, we have created an object of output stream using&nbsp;<code>FileOutputStream<\/code>. It is because&nbsp;<code>OutputStream<\/code>&nbsp;is an abstract class, so we cannot create an object of&nbsp;<code>OutputStream<\/code>.<\/p>\n\n\n\n<p><strong>Note<\/strong>: We can also create the output stream from other subclasses of the&nbsp;<code>OutputStream<\/code>&nbsp;class.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"methods\">Methods of OutputStream<\/h2>\n\n\n\n<p>The&nbsp;<code>OutputStream<\/code>&nbsp;class provides different methods that are implemented by its subclasses. Here are some of the methods:<\/p>\n\n\n\n<ul><li><code>write()<\/code>&nbsp;&#8211; writes the specified byte to the output stream<\/li><li><code>write(byte[] array)<\/code>&nbsp;&#8211; writes the bytes from the specified array to the output stream<\/li><li><code>flush()<\/code>&nbsp;&#8211; forces to write all data present in output stream to the destination<\/li><li><code>close()<\/code>&nbsp;&#8211; closes the output stream<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"example\">Example: OutputStream Using FileOutputStream<\/h2>\n\n\n\n<p>Here is how we can implement&nbsp;<code>OutputStream<\/code>&nbsp;using the&nbsp;<code>FileOutputStream<\/code>&nbsp;class.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.io.FileOutputStream;\nimport java.io.OutputStream;\n\npublic class Main {\n\n    public static void main(String args&#91;]) {\n        String data = \"This is a line of text inside the file.\";\n\n        try {\n            OutputStream out = new FileOutputStream(\"output.txt\");\n\n            \/\/ Converts the string into bytes\n            byte&#91;] dataBytes = data.getBytes();\n\n            \/\/ Writes data to the output stream\n            out.write(dataBytes);\n            System.out.println(\"Data is written to the file.\");\n\n            \/\/ Closes the output stream\n            out.close();\n        }\n\n        catch (Exception e) {\n            e.getStackTrace();\n        }\n    }\n}\n<\/code><\/pre>\n\n\n\n<p>In the above example, we have created an output stream using the&nbsp;<code>FileOutputStream<\/code>&nbsp;class. The output stream is now linked with the file&nbsp;<strong>output.txt<\/strong>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>OutputStream out = new FileOutputStream(\"output.txt\");\n<\/code><\/pre>\n\n\n\n<p>To write data to the&nbsp;<strong>output.txt<\/strong>&nbsp;file, we have implemented these methods.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>output.write();   \n   \/\/ To write data to the file\noutput.close();   \n   \/\/ To close the output stream\n<\/code><\/pre>\n\n\n\n<p>When we run the program, the&nbsp;<strong>output.txt<\/strong>&nbsp;file is filled with the following content.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>This is a line of text inside the file.<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we will learn about the Java OutputStream and its methods with the help of an example. The&nbsp;OutputStream&nbsp;class of the&nbsp;java.io&nbsp;package is an abstract superclass that represents an output stream of bytes. Since&nbsp;OutputStream&nbsp;is an abstract class, it is not useful by itself. However, its subclasses can be used to write data. Subclasses of OutputStream [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[483],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1286"}],"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=1286"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1286\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=1286"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=1286"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=1286"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}