Hurry! Try our new Interactive Courses for FREE. 🥳   🚀

FileWriter write() method in Java

In this tutorial, we will learn about the write() method of FileWriter class in Java. This method is used to write the data to the file. We can use any method among the overloading methods to write data to the file. This method can accept strings, characters, and the array of a character.

Syntax

These are the syntax declaration of overloading write() methods, this method accepts the complete String, simple character, and the array of character. This method is used to write the string into FileWriter.

public void write(String text)

This method is used to write a single char into FileWriter.

public void write(char c)

This method is used to write a char array into FileWriter.

public void write(char[] c)

Example 1: Write Data using FileWriter

In this example, we are implementing the example of the write() method of FileWriter class., this method accepts the String and writes it to the file. We have provided the output in the console as well as in the file after the code below to understand the concept of the write() method well.

import java.io.FileWriter;
import java.io.IOException;
class StudyTonight
{
	public static void main(String[] args) throws IOException 
	{ 
		try
		{    
			FileWriter fileWriter=new FileWriter("E:\\studytonight\\output.txt");    
			String str = "Hello Studytonight";
			fileWriter.write(str);    
			fileWriter.close();    
			System.out.println("Data Written to the file successfully");
		}
		catch(Exception e)
		{
			System.out.println("Error: "+e.toString());
		}    
	} 
}


Data Written to the file successfully

output.txt

Hello Studytonight

Example 2: Write Data using FileWriter

Here, we have a method write that accepts an array of characters only, after taking the array of characters it will write it to the file writer that we have. After writing data to the file we can check the data of the file to verify with the source data.

import java.io.FileWriter;
import java.io.IOException;
class StudyTonight
{
	public static void main(String[] args) throws IOException 
	{ 
		try
		{    
			FileWriter fileWriter=new FileWriter("E:\\studytonight\\output.txt");    
			char arr[] = {'S', 't', 'u', 'd', 'y', 't', 'o', 'n', 'i', 'g', 'h', 't'};
			fileWriter.write(arr);    
			fileWriter.close();    
			System.out.println("Data Written to the file successfully");
		}
		catch(Exception e)
		{
			System.out.println("Error: "+e.toString());
		}    
	} 
}


Data Written to the file successfully

output.txt

Studytonight

Example 3: Write Data using FileWriter

This example is similar to what we have seen in the above examples, the method accepts the character and writes it to the file. We have provided the output in the console as well as in the file after the code below to understand the concept of the write() method well.

import java.io.FileWriter;
import java.io.IOException;
class StudyTonight
{
	public static void main(String[] args) throws IOException 
	{ 
		try
		{    
			FileWriter fileWriter=new FileWriter("E:\\studytonight\\output.txt");    
			char c='A';
			fileWriter.write(c);    
			fileWriter.close();    
			System.out.println("Data Written to the file successfully");
		}
		catch(Exception e)
		{
			System.out.println("Error: "+e.toString());
		}    
	} 
}


Data Written to the file successfully

output.txt

A

Conclusion

In this tutorial, we learned about the write() method of FileWriter class. This method is used to write the data to the file. We can use any method among the overloading methods to write data to the file. This method can accept strings, characters, and the array of a character.