In this tutorial, we will learn about Java FileWriter and its strategies with the help of examples.
The FileWriter class of the java.io package can be used to write data (in characters) to files.
It extends the OutputStreamWriter class.
In this article, you will learn-
Make a FileWriter
To make a file writer, we should import the Java.io.FileWriter package first. When we import the package, here is the manner by which we can make the file writer.
- Using the name of the file
FileWriter output = new FileWriter(String name);
Here, we have made a file writer that will be connected to the record determined by the name.
2. Using an object of the file
FileWriter input = new FileWriter(File fileObj);
Here, we have made a record writer that will be connected to the document determined by the object of the file.
In the above example, the data are stored using some default character encoding.
However, since Java 11 we can determine the kind of character encoding (UTF8 or UTF16) also.
FileWriter input = new FileWriter(String file, Charset cs);
Here, we have used the Charset class to indicate the character encoding of the record writer.
Strategies for FileWriter
The FileWriter class gives executions to various strategies present in the Writer class.
write() Method
- write() – writes a single character to the writer
- write(char[] array) – writes the characters from the specified array to the writer
- write(String data) – writes the specified string to the writer
Example: FileWriter to write data to a File
import java.io.FileWriter;
public class Main {
public static void main(String args[]) {
String data = "This is the data in the output file";
try {
// Creates a FileWriter
FileWriter output = new FileWriter("output.txt");
// Writes the string to the file
output.write(data);
// Closes the writer
output.close();
}
catch (Exception e) {
e.getStackTrace();
}
}
}
In the above example, we have made a file writer named output. The output reader is connected with the output.txt record.
FileWriter output = new FileWriter("output.txt");
To write data to the file, we have used the write() technique.
Here when we run the program, the output.txt file is loaded up with the accompanying substance.
This is a line of text inside the file.
getEncoding() Method
The getEncoding() strategy can be used to get the type of encoding that is used to write data. For instance,
import java.io.FileWriter;
import java.nio.charset.Charset;
class Main {
public static void main(String[] args) {
String file = "output.txt";
try {
// Creates a FileReader with default encoding
FileWriter output1 = new FileWriter(file);
// Creates a FileReader specifying the encoding
FileWriter output2 = new FileWriter(file, Charset.forName("UTF8"));
// Returns the character encoding of the reader
System.out.println("Character encoding of output1: " + output1.getEncoding());
System.out.println("Character encoding of output2: " + output2.getEncoding());
// Closes the reader
output1.close();
output2.close();
}
catch(Exception e) {
e.getStackTrace();
}
}
}
Output
The character encoding of output1: Cp1252
The character encoding of output2: UTF8
In the above example, we have made 2 file writer named output1 and output2.
output1 doesn’t indicate the character encoding. Subsequently, the getEncoding() technique returns the default character encoding.
output2 indicates the character encoding, UTF8. Consequently, the getEncoding() technique returns the predetermined character encoding.
Note: We have used the Charset.forName() method to specify the type of character encoding. To learn more, visit Java Charset (official Java documentation).
close() Method
To close the record writer, we can use the close() strategy. When the close() strategy is called, we can’t use the writer to write the data.
Other methods of FileWriter
Method | Description |
flush() | forces to write all the data present in the writer to the corresponding destination |
append() | inserts the specified character to the current writer |
To learn more, visit Java FileWriter (official Java documentation).
Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.
i love this perfect article