Write to a file in Java

 
When you are dealing with Files, you may need to write to a file in Java. In this tutorial we will see how to write to a file.

In Java, we can write to a file in different ways. Let’s see how to write to a file using FileWriter class.

Using FileWriter:

FileWriter  is a class for writing streams of characters to a File. FileWriter class provides many constructors to create objects. This class provides many methods for file operations but we will use write() method to write to a file. This method writes a string.

Syntax:

Let’s see how to do it.

Output:

tempFile.txt:

Related posts

Leave a Comment