Java file writer Examples

FileWriter class is introduced in JDK1.1.This is used for writing character files.

This class inherits from the OutputStream class.

FileWriter class is a part of the java.io package.

FileWriter has the following Constructors

FileWriter(File file)
It Constructs a FileWriter object given a File object.

FileWriter(File file, boolean append)
Constructs a FileWriter object given a file name with a boolean indicating whether or not to append the data written.

FileWriter(FileDescriptor fd)
It Constructs a FileWriter object associated with a file descriptor.

FileWriter(String fileName)
It Constructs a FileWriter object given a file name.

FileWriter(String fileName, boolean append)
It Constructs a FileWriter object given a file name with a boolean indicating whether or not to append the data written.

Using BufferedWriter to write a String to a new file

import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.BufferedWriter; public class FileWriterExample { public static void main(String[] args) throws IOException { String str = "Hello World!!!"; BufferedWriter writer = new BufferedWriter(new FileWriter("abc.txt")); writer.write(str); writer.close(); System.out.println("File Writing successful."); } } output: File Writing successful.

Methods in FileWriter

public void write (int c) throws IOException
Writes a single character.

public void write (char [] stir) throws IOException
Writes an array of characters.

public void write(String str)throws IOException  
Writes a string.

public void write(String str, int off, int len)throws IOException
Writes a portion of a string. Here off is offset from which to start writing characters and len is the number of characters to write.

public void flush() throws IOException
flushes the stream.

public void close() throws IOException
flushes the stream first and then closes the writer.

Create a text file using FileWriter

import java.io.FileWriter; import java.io.IOException; public class fileWriterExample { public static void main(String[] args) throws IOException { String str = "Java file Writer example."; FileWriter fw = new FileWriter("abc.txt"); for (int i = 0; i < str.length(); i++) fw.write(str.charAt(i)); System.out.println("File Writing successful."); fw.close(); } }

Append a string to the existing file

public void appendFile() throws IOException { String str = "Welcome to programming world!!!"; BufferedWriter writer = new BufferedWriter(new FileWriter("C:\\files\\abc.txt", true)); writer.append("\n"); writer.append(str); writer.close(); }

Post/Questions related to Java file writer Example

How to download a pdf file using java?
Create New File in Java
Read file to byte[] array in Java Java File Reader Examples
How to Download File from URL in Java
java.nio.file.Files methods with Examples
Java File Path, Absolute Path and Relative Path
Path toString() method in Java

In this article, we have seen Java file writer Examples. All source code in the article can be found in the GitHub repository.