{"id":1304,"date":"2022-02-27T06:15:42","date_gmt":"2022-02-27T06:15:42","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=1304"},"modified":"2022-02-27T06:15:42","modified_gmt":"2022-02-27T06:15:42","slug":"file-writer-class","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/02\/27\/file-writer-class\/","title":{"rendered":"File Writer Class"},"content":{"rendered":"\n<p>In this tutorial, we will learn about Java FileWriter and its methods with the help of examples.<\/p>\n\n\n\n<p id=\"introduction\">The&nbsp;<code>FileWriter<\/code>&nbsp;class of the&nbsp;<code>java.io<\/code>&nbsp;package can be used to write data (in characters) to files.<\/p>\n\n\n\n<p>It extends the&nbsp;<code>OutputStreamWriter<\/code>&nbsp;class.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.programiz.com\/sites\/tutorial2program\/files\/java-filewriter.png\" alt=\"The FileWriter is a subclass of OutputStreamWriter and the OutputStreamWriter is subclass of the Java Writer.\" title=\"Java FileWriter Class\"\/><\/figure>\n\n\n\n<p>Before you learn more about\u00a0<code>FileWriter<\/code>, make sure to know about\u00a0Java File.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"create\">Create a FileWriter<\/h2>\n\n\n\n<p>In order to create a file writer, we must import the&nbsp;<code>Java.io.FileWriter<\/code>&nbsp;package first. Once we import the package, here is how we can create the file writer.<\/p>\n\n\n\n<p><strong>1. Using the name of the file<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>FileWriter output = new FileWriter(String name);\n<\/code><\/pre>\n\n\n\n<p>Here, we have created a file writer that will be linked to the file specified by the&nbsp;<var>name<\/var>.<\/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>FileWriter  input = new FileWriter(File fileObj);\n<\/code><\/pre>\n\n\n\n<p>Here, we have created a file writer that will be linked to the file specified by the object of the file.<\/p>\n\n\n\n<p>In the above example, the data are stored using some default character encoding.<\/p>\n\n\n\n<p>However, since Java 11 we can specify the type of character encoding (<strong>UTF8<\/strong>&nbsp;or&nbsp;<strong>UTF16<\/strong>) as well.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>FileWriter input = new FileWriter(String 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 character encoding of the file writer.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Methods of FileWriter<\/h2>\n\n\n\n<p>The&nbsp;<code>FileWriter<\/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: FileWriter to write data to a File<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.io.FileWriter;\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 FileWriter\n      FileWriter output = new FileWriter(\"output.txt\");\n\n      \/\/ Writes the 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 file writer named&nbsp;<var>output<\/var>. The output reader is linked with the&nbsp;<strong>output.txt<\/strong>&nbsp;file.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>FileWriter output = new FileWriter(\"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<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. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.io.FileWriter;\nimport java.nio.charset.Charset;\n\nclass Main {\n  public static void main(String&#91;] args) {\n\n    String file = \"output.txt\";\n\n    try {\n      \/\/ Creates a FileReader with default encoding\n      FileWriter output1 = new FileWriter(file);\n\n      \/\/ Creates a FileReader specifying the encoding\n      FileWriter output2 = new FileWriter(file, Charset.forName(\"UTF8\"));\n\n      \/\/ Returns the character encoding of the reader\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 file 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 file 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 FileWriter<\/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 FileWriter and its methods with the help of examples. The&nbsp;FileWriter&nbsp;class of the&nbsp;java.io&nbsp;package can be used to write data (in characters) to files. It extends the&nbsp;OutputStreamWriter&nbsp;class. Before you learn more about\u00a0FileWriter, make sure to know about\u00a0Java File. Create a FileWriter In order to create a file writer, we [&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\/1304"}],"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=1304"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1304\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=1304"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=1304"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=1304"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}