Java FileWriter

The Java FileWriter class is for writing the text to the character-based files using a default buffer size. It uses character encoding default to the platform, if not provided otherwise.

FileWriter is usually wrapped by higher-level Writer types, such as BufferedWriter or PrintWriter. FileWriter provides better performance and higher-level, more flexible methods to write content.

1. Java FileWriter class

  • The FileWriter is used for writing to the character based files. Pass the required charset, if we do not wish to use platform’s default charset.
  • FileWriter is part of java.io package.
  • FileWriter extends the abstract class Writer.
  • FileWriter implements Closeable, AutoCloseable, Flushable and Appendable interfaces.
  • FileWriter provides methods for writing to a new file, and appending to an existing file.
  • If we try to open a file, which is already opened, the constructors of FileWriter class will fail.

2. Creating a FileWriter

To create FileWriter, use one of its constructors. All constructors will need at least the file name or File object referring to the file where we want to write the text.

Setting the Charset information is optional. If not provided, system’s default charset will be used.

String fileName = "dataOut.txt";
File file = new File(fileName);

// Using file name
FileWriter fw1 = new FileWriter(fileName);

// Using File object
FileWriter fw2 = new FileWriter(file);

3. Setting Character Encoding

If we want to write the characters in a different encoding then pass the Charset information in FileWriter‘s constructor.

String fileName = "dataOut.txt";
File file = new File(fileName);

FileWriter fw = new FileWriter(file, Charset.forName("UTF8"));

4. Closing the FileWriter

Call the fileWriter.close() method when we are done with writing to the file. Or we can use the auto-closable feature of this class.

In the given example, try-with-resources feature will close the FileWriter automatically when the try block is completely executed.

String fileName = "dataOut.txt";

try (FileWriter fw 
		= new FileWriter(new File(fileName))) {

	//Perform operations
}

5. Java FileWriter Example

Lets see a few examples of writing to a file using the FileWriter in Java. In each example, we will write the file dataOut.txt with the content "humpty dumpty".

Example 1: Creating a new file and Writing to it using FileWriter

In the given example, we opened a new file for writing the content. After the program executed, a new file dataOut.txt is created and the content "humpty dumpty" is written on it.

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class FileWriterExample 
{
	public static void main(String[] args) 
							throws IOException 
	{
		String fileName = "dataOut.txt";

		try (FileWriter fw 
				= new FileWriter(new File(fileName))) {

			fw.write("humpty dumpty");
		}
	}
}

Program output:

humpty dumpty

Example 2: Appending to an existing file using FileWriter

To append to a file, open the file in append mode by passing the value true to the constructor of FileWriter. Once the file is opened in append mode, use the various append methods to append the test to existing content in the file.

package com.howtodoinjava.io;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class FileWriterExample 
{
	public static void main(String[] args) 
							throws IOException 
	{
		String fileName = "dataOut.txt";
	
		try (FileWriter fw 
				= new FileWriter(new File(fileName), true)) {

			fw.append(" sat on a wall");
		}
	}
}

Program output:

humpty dumpty sat on a wall

Happy Learning !!

Comments

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode