{"id":1296,"date":"2022-02-27T06:04:23","date_gmt":"2022-02-27T06:04:23","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=1296"},"modified":"2022-02-27T06:04:23","modified_gmt":"2022-02-27T06:04:23","slug":"writer-class","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/02\/27\/writer-class\/","title":{"rendered":"Writer Class"},"content":{"rendered":"\n<p>In this tutorial, we will learn about Java Writer, its subclasses and its methods with the help of an example.<\/p>\n\n\n\n<p id=\"introduction\">The&nbsp;<code>Writer<\/code>&nbsp;class of the&nbsp;<code>java.io<\/code>&nbsp;package is an abstract superclass that represents a stream of characters.<\/p>\n\n\n\n<p>Since&nbsp;<code>Writer<\/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 Writer<\/h2>\n\n\n\n<p>In order to use the functionality of the&nbsp;<code>Writer<\/code>, we can use its subclasses. Some of them are:<\/p>\n\n\n\n<ul><li>BufferedWriter<\/li><li>OutputStreamWriter<\/li><li>FileWriter<\/li><li>StringWriter<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.programiz.com\/sites\/tutorial2program\/files\/java-writer-class.png\" alt=\"Subclasses of Java Writer are BufferedWriter, OutputStreamWriter, FileWriter and StringWriter.\" title=\"Java Writer Class\"\/><\/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 a Writer<\/h2>\n\n\n\n<p>In order to create a&nbsp;<code>Writer<\/code>, we must import the&nbsp;<code>java.io.Writer<\/code>&nbsp;package first. Once we import the package, here is how we can create the writer.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Creates a Writer\nWriter output = new FileWriter();\n<\/code><\/pre>\n\n\n\n<p>Here, we have created a writer named output using the&nbsp;<code>FileWriter<\/code>&nbsp;class. It is because the&nbsp;<code>Writer<\/code>&nbsp;is an abstract class. Hence we cannot create an object of&nbsp;<code>Writer<\/code>.<\/p>\n\n\n\n<p><strong>Note<\/strong>: We can also create writers from other subclasses of the&nbsp;<code>Writer<\/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 Writer<\/h2>\n\n\n\n<p>The&nbsp;<code>Writer<\/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(char[] array)<\/code>&nbsp;&#8211; writes the characters from the specified array to the output stream<\/li><li><code>write(String data)<\/code>&nbsp;&#8211; writes the specified string to the writer<\/li><li><code>append(char c)<\/code>&nbsp;&#8211; inserts the specified character to the current writer<\/li><li><code>flush()<\/code>&nbsp;&#8211; forces to write all the data present in the writer to the corresponding destination<\/li><li><code>close()<\/code>&nbsp;&#8211; closes the writer<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"example\">Example: Writer Using FileWriter<\/h2>\n\n\n\n<p>Here is how we can implement the&nbsp;<code>Writer<\/code>&nbsp;using the&nbsp;<code>FileWriter<\/code>&nbsp;class.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.io.FileWriter;\nimport java.io.Writer;\n\npublic class Main {\n\n    public static void main(String args&#91;]) {\n\n        String data = \"This is the data in the output file\";\n\n        try {\n            \/\/ Creates a Writer using FileWriter\n            Writer output = new FileWriter(\"output.txt\");\n\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 a writer using the&nbsp;<code>FileWriter<\/code>&nbsp;class. The writer is linked with the file&nbsp;<strong>output.txt<\/strong>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Writer output = new FileWriter(\"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 writer\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 Java Writer, its subclasses and its methods with the help of an example. The&nbsp;Writer&nbsp;class of the&nbsp;java.io&nbsp;package is an abstract superclass that represents a stream of characters. Since&nbsp;Writer&nbsp;is an abstract class, it is not useful by itself. However, its subclasses can be used to write data. Subclasses of Writer [&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\/1296"}],"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=1296"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1296\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=1296"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=1296"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=1296"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}