{"id":1290,"date":"2022-02-26T07:50:56","date_gmt":"2022-02-26T07:50:56","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=1290"},"modified":"2022-02-26T07:50:56","modified_gmt":"2022-02-26T07:50:56","slug":"file-output-stream-class","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/02\/26\/file-output-stream-class\/","title":{"rendered":"File Output Stream Class"},"content":{"rendered":"\n<p>In this tutorial, we will learn about Java FileOutputStream and its methods with the help of examples.<\/p>\n\n\n\n<p id=\"introduction\">The&nbsp;<code>FileOutputStream<\/code>&nbsp;class of the&nbsp;<code>java.io<\/code>&nbsp;package can be used to write data (in bytes) to the files.<\/p>\n\n\n\n<p>It extends the&nbsp;<code>OutputStream<\/code>&nbsp;abstract class.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.programiz.com\/sites\/tutorial2program\/files\/java-fileoutputstream.png\" alt=\"The FileOutputStream class is the subclass of the Java OutputStream.\" title=\"Java FileOutputStream Class\"\/><\/figure>\n\n\n\n<p>Before you learn about\u00a0<code>FileOutputStream<\/code>, make sure to know about\u00a0Java Files.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"create\">Create a FileOutputStream<\/h2>\n\n\n\n<p>In order to create a file output stream, we must import the&nbsp;<code>java.io.FileOutputStream<\/code>&nbsp;package first. Once we import the package, here is how we can create a file output stream in Java.<\/p>\n\n\n\n<p><strong>1. Using the path to file<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Including the boolean parameter\nFileOutputStream output = new FileOutputStream(String path, boolean value);\n\n\/\/ Not including the boolean parameter\nFileOutputStream output = new FileOutputStream(String path);\n<\/code><\/pre>\n\n\n\n<p>Here, we have created an output stream that will be linked to the file specified by the&nbsp;<var>path<\/var>.<\/p>\n\n\n\n<p>Also,&nbsp;<var>value<\/var>&nbsp;is an optional boolean parameter. If it is set to&nbsp;<code>true<\/code>, the new data will be appended to the end of the existing data in the file. Otherwise, the new data overwrites the existing data in the file.<\/p>\n\n\n\n<p><strong>2. Using an object of the file<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>FileOutputStream output = new FileOutputStream(File fileObject);\n<\/code><\/pre>\n\n\n\n<p>Here, we have created an output stream that will be linked to the file specified by&nbsp;<code>fileObject<\/code>.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Methods of FileOutputStream<\/h2>\n\n\n\n<p>The&nbsp;<code>FileOutputStream<\/code>&nbsp;class provides implementations for different methods present in the&nbsp;<code>OutputStream<\/code>&nbsp;class.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"write\">write() Method<\/h3>\n\n\n\n<ul><li><code>write()<\/code>&nbsp;&#8211; writes the single&nbsp;<var>byte<\/var>&nbsp;to the file 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>write(byte[] array, int start, int length)<\/code>&nbsp;&#8211; writes the number of bytes equal to&nbsp;<var>length<\/var>&nbsp;to the output stream from an array starting from the position&nbsp;<var>start<\/var><\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Example: FileOutputStream to write data to a File<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.io.FileOutputStream;\n\npublic class Main {\n    public static void main(String&#91;] args) {\n        \n        String data = \"This is a line of text inside the file.\";\n\n        try {\n            FileOutputStream output = new FileOutputStream(\"output.txt\");\n\n            byte&#91;] array = data.getBytes();\n\n            \/\/ Writes byte to the file\n            output.write(array);\n\n            output.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 a file output stream named&nbsp;<var>output<\/var>. The file output stream is linked with the file&nbsp;<strong>output.txt<\/strong>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>FileOutputStream output = new FileOutputStream(\"output.txt\");\n<\/code><\/pre>\n\n\n\n<p>To write data to the file, we have used the&nbsp;<code>write()<\/code>&nbsp;method.<\/p>\n\n\n\n<p>Here, 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.\n<\/code><\/pre>\n\n\n\n<p><strong>Note<\/strong>: The&nbsp;<code>getBytes()<\/code>&nbsp;method used in the program converts a string into an array of bytes.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"flush\">flush() Method<\/h3>\n\n\n\n<p>To clear the output stream, we can use the&nbsp;<code>flush()<\/code>&nbsp;method. This method forces the output stream to write all data to the destination. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.io.FileOutputStream;\nimport java.io.IOException;\n\npublic class Main {\n    public static void main(String&#91;] args) throws IOException {\n\n        FileOutputStream out = null;\n        String data = \"This is demo of flush method\";\n\n        try {\n            out = new FileOutputStream(\" flush.txt\");\n\n            \/\/ Using write() method\n            out.write(data.getBytes());\n\n            \/\/ Using the flush() method\n            out.flush();\n            out.close();\n        }\n        catch(Exception e) {\n            e.getStackTrace();\n        }\n    }\n}\n<\/code><\/pre>\n\n\n\n<p>When we run the program, the file&nbsp;<strong>flush.txt<\/strong>&nbsp;is filled with the text represented by the string&nbsp;<code>data<\/code>.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">close() Method<\/h3>\n\n\n\n<p>To close the file output stream, we can use the&nbsp;<code>close()<\/code>&nbsp;method. Once the method is called, we cannot use the methods of&nbsp;<code>FileOutputStream<\/code>.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"other-methods\">Other Methods Of FileOutputStream<\/h2>\n\n\n\n<figure class=\"wp-block-table is-style-regular\"><table><tbody><tr><th>Methods<\/th><th>Descriptions<\/th><\/tr><tr><td><code>finalize()<\/code><\/td><td>ensures that the <code>close()<\/code> method is called<\/td><\/tr><tr><td><code>getChannel()<\/code><\/td><td>returns the object of <code>FileChannel<\/code> associated with the output stream<\/td><\/tr><tr><td><code>getFD()<\/code><\/td><td>returns the file descriptor associated with the output stream<\/td><\/tr><\/tbody><\/table><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we will learn about Java FileOutputStream and its methods with the help of examples. The&nbsp;FileOutputStream&nbsp;class of the&nbsp;java.io&nbsp;package can be used to write data (in bytes) to the files. It extends the&nbsp;OutputStream&nbsp;abstract class. Before you learn about\u00a0FileOutputStream, make sure to know about\u00a0Java Files. Create a FileOutputStream In order to create a file output [&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\/1290"}],"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=1290"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1290\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=1290"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=1290"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=1290"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}