How to Write Data into Text Files in Java

Published: 25 November 2025
on channel: Test Automation Academy 2.0
33
1

Learn how to write data into a text file using Java in this step-by-step tutorial! 📄💻
In this video, I explain the FileWriter class, try-with-resources, and how to handle exceptions while writing data to a text file. This tutorial is perfect for beginners who want to learn Java file handling and improve their programming skills.

What you’ll learn in this video:
✅ How to create and write to a text file in Java
✅ Using FileWriter to write text data
✅ Handling exceptions with IOException
✅ Using newline characters (\n) to format file content
✅ Tips for appending data or using BufferedWriter for efficiency

Code Example:

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

public class WriteTextFile {
public static void main(String[] args) {
String filePath = "output.txt";
String data = "Hello World!\nWelcome to Java File Handling.";

try (FileWriter fw = new FileWriter(filePath)) {
fw.write(data);
System.out.println("Data written successfully!");
} catch (IOException e) {
e.printStackTrace();
}
}
}


Recommended for:

Java beginners

Students learning file handling

Anyone preparing for Java interviews

💡 Don’t forget to Like 👍, Share 🔄, and Subscribe 🔔 for more Java tutorials and programming tips!

#JavaTutorial #FileHandling #JavaBeginners #WriteTextFile #ProgrammingInJava


On this page of the site you can watch the video online How to Write Data into Text Files in Java with a duration of hours minute second in good quality, which was uploaded by the user Test Automation Academy 2.0 25 November 2025, share the link with friends and acquaintances, this video has already been watched 33 times on youtube and it was liked by 1 viewers. Enjoy your viewing!