Table of Contents
The FileWriter class of java.io package makes writing to files in Java as easy as A-B-C. The FileWriter class writes to files as a stream of characters instead of bytes(as in FileOutputStream). Like its input-oriented counterpart FileReader, the FileWriter class is intended for writing “text” based files one character at a time.
Let us look into how FileWriter works in Java.
Constructors
The FileWriter class in Java has five parameterized constructors to allow programmers to manipulate or write to files. Based on the mode of writing, files can be overwritten or appended. Let us take a look at the constructors defined in the FileWriter class.
- FileWriter(File file) – This constructor accepts a java.io.File object as a parameter and opens the file for writing. By default, this overwrites any previous content of the file.
- FileWriter(FileDescriptor descriptor) – This constructor creates an object of FileWriter which accepts a java.io.FileDescriptor object as a parameter and opens the file referenced by the descriptor object. By default, it overwrites the previous contents.
- FileWriter(String filePath) – This constructor accepts a java.lang.String object as a parameter and opens the file referenced by the path or URI contained in the String and overwrites any existing content in the file.
- FileWriter(File file, boolean append) – This constructor opens accepts a java.io.File and a boolean value as parameter. If the boolean value is true, the existing contents are not overwritten and the new contents are appended
- FileWriter(String filePath, boolean append) – This constructor opens accepts a java.lang.String and a boolean value as parameter. If the boolean value is true, the existing contents are not overwritten and the new contents are appended.
In both of the last two cases, if the boolean value entered is ‘false’, the contents are overwritten.
If the file is not found or cannot be opened, a FileNotFoundException is thrown.
Methods
The FileWriter class contains methods that allow the programmer to write characters/text to a file. The FileWriter object writes the contents of the file one character at a time.
Let us take a look at the methods we use to write using FileWriter.
- public void write(int charVal) – This method writes a single character(based on the ASCII value of the input) to the file.
- public void write(char arr[],int offset,int l) – This method writes the characters from the character array from the offset position, for a length ‘l’ of characters.
- public void write(String str, int offset, int l) – This method writes the characters from the String object from the offset position for a length ‘l’ of characters.
- public void flush() – Flushes the current FileWriter object.
- public void close() – Closes the FileWriter object to prevent any resource leaks.
Example
Let us look at an example of writing to a file with FileWriter class.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
import java.io.FileReader; import java.io.FileWriter; public class TestFileWriter { public static void main(String args[]){ String filePath = "C:/Users/Me/Desktop/sample.txt"; try{ FileWriter writeNew = new FileWriter(filePath); writeNew.write("Hello. I'm writing this new. "); writeNew.close(); System.out.println("New Write Complete. File Reads: \t"); readFileNow(filePath); FileWriter appendOld = new FileWriter(filePath,true); appendOld.write("Append works too"); appendOld.close(); System.out.println("Append Complete. File Reads: \t"); readFileNow(filePath); }catch(Exception e){ e.printStackTrace(); } } public static void readFileNow(String filePath){ try{ FileReader fileReader = new FileReader(filePath); int charRead = 0; while(charRead != -1){ charRead = fileReader.read(); System.out.print((char)charRead); } fileReader.close(); }catch(Exception e){ e.printStackTrace(); } } } |
Output:
Append Complete. File Reads
Hello. I’m writing this new. Append works too
Thats all about java FileWriter example.