In this tutorial, we will learn about the Java FileWriter, its constructors and its methods with the help of an example.
This is a Convenience class for writing character files. The constructors of this class assume that the default character encoding and the default byte-buffer size are acceptable. To specify these values yourself, construct an OutputStreamWriter on a FileOutputStream.
Methods inherited from class java.io.OutputStreamWriter
close, flush, getEncoding, write
Methods inherited from class java.io.Writer
append, append, append, write
import java.io.FileWriter;
public class Codemistic
{
public static void main(String args[])
{
String data = "This file consists of a single line.";
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();
}
}
}
Output :
A file will be created named output.txt that contains the following line:
This file consists of a single line.