{"id":1300,"date":"2022-02-27T06:08:33","date_gmt":"2022-02-27T06:08:33","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=1300"},"modified":"2022-02-27T06:08:33","modified_gmt":"2022-02-27T06:08:33","slug":"output-stream-writer-class","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/02\/27\/output-stream-writer-class\/","title":{"rendered":"Output Stream Writer Class"},"content":{"rendered":"\n<p>In this tutorial, we will learn about Java OutputStreamWriter and its methods with the help of examples.<\/p>\n\n\n\n<p id=\"introduction\">The&nbsp;<code>OutputStreamWriter<\/code>&nbsp;class of the&nbsp;<code>java.io<\/code>&nbsp;package can be used to convert data in character form into data in bytes form.<\/p>\n\n\n\n<p>It extends the abstract class&nbsp;<code>Writer<\/code>.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.programiz.com\/sites\/tutorial2program\/files\/java-%20output-stream-writer.png\" alt=\"The OutputStreamWriter is a subclass of the Java Writer.\" title=\"Java OutputStreamWriter Class\"\/><\/figure>\n\n\n\n<p>The&nbsp;<code>OutputStreamWriter<\/code>&nbsp;class works with other output streams. It is also known as a bridge between byte streams and character streams. This is because the&nbsp;<code>OutputStreamWriter<\/code>&nbsp;converts its characters into bytes.<\/p>\n\n\n\n<p>For example, some characters require 2 bytes to be stored in the storage. To write such data we can use the output stream writer that converts the character into corresponding bytes and stores the bytes together.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"create\">Create an OutputStreamWriter<\/h2>\n\n\n\n<p>In order to create an&nbsp;<code>OutputStreamWriter<\/code>, we must import the&nbsp;<code>java.io.OutputStreamWriter<\/code>&nbsp;package first. Once we import the package here is how we can create the output stream writer.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Creates an OutputStream\nFileOutputStream file = new FileOutputStream(String path);\n\n\/\/ Creates an OutputStreamWriter\nOutputStreamWriter output = new OutputStreamWriter(file);\n<\/code><\/pre>\n\n\n\n<p>In the above example, we have created an&nbsp;<code>OutputStreamWriter<\/code>&nbsp;named output along with the&nbsp;<code>FileOutputStream<\/code>&nbsp;named file.<\/p>\n\n\n\n<p>Here, we are using the default character encoding to write characters to the output stream.<\/p>\n\n\n\n<p>However, we can specify the type of character encoding (<strong>UTF8<\/strong>&nbsp;or&nbsp;<strong>UTF16<\/strong>) to be used to write data.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Creates an OutputStreamWriter specifying the character encoding\nOutputStreamWriter output = new OutputStreamWriter(file, Charset cs);\n<\/code><\/pre>\n\n\n\n<p>Here, we have used the&nbsp;<code>Charset<\/code>&nbsp;class to specify the type of character encoding.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Methods of OutputStreamWriter<\/h2>\n\n\n\n<p>The&nbsp;<code>OutputStreamWriter<\/code>&nbsp;class provides implementations for different methods present in the&nbsp;<code>Writer<\/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 a single character to the writer<\/li><li><code>write(char[] array)<\/code>&nbsp;&#8211; writes the characters from the specified array to the writer<\/li><li><code>write(String data)<\/code>&nbsp;&#8211; writes the specified string to the writer<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Example: OutputStreamWriter to write data to a File<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.io.FileOutputStream;\nimport java.io.OutputStreamWriter;\n\npublic class Main {\n\n  public static void main(String args&#91;]) {\n\n    String data = \"This is a line of text inside the file.\";\n\n    try {\n      \/\/ Creates a FileOutputStream\n      FileOutputStream file = new FileOutputStream(\"output.txt\");\n\n      \/\/ Creates an OutputStreamWriter\n      OutputStreamWriter output = new OutputStreamWriter(file);\n\n      \/\/ Writes string to the file\n      output.write(data);\n\n      \/\/ Closes the writer\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 an output stream reader using the file output stream. The output stream reader is linked with the&nbsp;<strong>output.txt<\/strong>&nbsp;file.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>FileOutputStream file = new FileOutputStream(\"output.txt\");\nOutputStreamWriter output = new OutputStreamWriter(file);\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<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"getEncoding\">getEncoding() Method<\/h2>\n\n\n\n<p>The&nbsp;<code>getEncoding()<\/code>&nbsp;method can be used to get the type of encoding that is used to write data to the output stream. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.io.OutputStreamWriter;\nimport java.nio.charset.Charset;\nimport java.io.FileOutputStream;\n\nclass Main {\n  public static void main(String&#91;] args) {\n\n    try {\n      \/\/ Creates an output stream\n      FileOutputStream file = new FileOutputStream(\"output.txt\");\n\n      \/\/ Creates an output stream reader with default encoding\n      OutputStreamWriter output1 = new OutputStreamWriter(file);\n\n      \/\/ Creates an output stream reader specifying the encoding\n      OutputStreamWriter output2 = new OutputStreamWriter(file, Charset.forName(\"UTF8\"));\n\n      \/\/ Returns the character encoding of the output stream\n      System.out.println(\"Character encoding of output1: \" + output1.getEncoding());\n      System.out.println(\"Character encoding of output2: \" + output2.getEncoding());\n\n      \/\/ Closes the reader\n      output1.close();\n      output2.close();\n    }\n\n    catch(Exception e) {\n      e.getStackTrace();\n    }\n  }\n}\n<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>The character encoding of output1: Cp1252\nThe character encoding of output2: UTF8\n<\/samp><\/code><\/pre>\n\n\n\n<p>In the above example, we have created 2 output stream writer named&nbsp;<var>output1<\/var>&nbsp;and&nbsp;<var>output2<\/var>.<\/p>\n\n\n\n<ul><li><var>output1<\/var>&nbsp;does not specify the character encoding. Hence the&nbsp;<code>getEncoding()<\/code>&nbsp;method returns the default character encoding.<\/li><li><var>output2<\/var>&nbsp;specifies the character encoding,&nbsp;<strong>UTF8<\/strong>. Hence the&nbsp;<code>getEncoding()<\/code>&nbsp;method returns the specified character encoding.<\/li><\/ul>\n\n\n\n<p><strong>Note<\/strong>: We have used the\u00a0<code>Charset.forName()<\/code>\u00a0method to specify the type of character encoding. <\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">close() Method<\/h2>\n\n\n\n<p>To close the output stream writer, we can use the&nbsp;<code>close()<\/code>&nbsp;method. Once the&nbsp;<code>close()<\/code>&nbsp;method is called, we cannot use the writer to write the data.<\/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 Output Stream Writer<\/h2>\n\n\n\n<figure class=\"wp-block-table is-style-regular\"><table><tbody><tr><th>Method<\/th><th>Description<\/th><\/tr><tr><td><code>flush()<\/code><\/td><td>forces to write all the data present in the writer to the corresponding destination<\/td><\/tr><tr><td><code>append()<\/code><\/td><td>inserts the specified character to the current writer<\/td><\/tr><\/tbody><\/table><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we will learn about Java OutputStreamWriter and its methods with the help of examples. The&nbsp;OutputStreamWriter&nbsp;class of the&nbsp;java.io&nbsp;package can be used to convert data in character form into data in bytes form. It extends the abstract class&nbsp;Writer. The&nbsp;OutputStreamWriter&nbsp;class works with other output streams. It is also known as a bridge between byte streams [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[532],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1300"}],"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=1300"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1300\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=1300"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=1300"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=1300"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}