w3schools Java Write to File Tutorial Easy Guide

Robert Jul 13, 2026

When working with Java, there often comes a time when you need to write data to a file. This could be for various reasons such as logging, data storage, or even creating a new file from scratch. Java offers several ways to accomplish this, and in this guide, we'll delve into the most common and efficient methods.

10 Examples to Read a Text file in Java [UPDATED]
10 Examples to Read a Text file in Java [UPDATED]

Before we dive into the detailed code snippets, it's crucial to understand that in Java, we can't write directly to a file. We need to use a character stream or a byte stream. In this tutorial, we'll use a FileWriter, which is a type of character stream.

Perfect Java note for new learners
Perfect Java note for new learners

Using FileWriter to Write to a File in Java

The FileWriter class allows us to create a new file and write strings to it. It's a simple and efficient way to write to a file in Java. Here's a basic example:

Java Scanner Class Made Easy: Your Ultimate Input Cheat Sheet 🎯💻
Java Scanner Class Made Easy: Your Ultimate Input Cheat Sheet 🎯💻

FileWriter writer = new FileWriter("filename.txt");
writer.write("Hello, file!");
writer.close();

Writing Multiple Lines

GENERICS IN JAVA
GENERICS IN JAVA

While the previous example writes a single string to the file, sometimes you might need to write multiple lines. You can achieve this by using the println method provided by the PrintWriter class, which automatically adds a newline character after each call.

PrintWriter writer = new PrintWriter(new FileWriter("filename.txt"));
writer.println("Line 1");
writer.println("Line 2");
writer.close();

Appending to an Existing File

Instead of overwriting the contents of an existing file, we might want to append data to it. This can be done by setting the second parameter of the FileWriter constructor to true.

FileWriter writer = new FileWriter("filename.txt", true);

Java Patterns Notes 1
Java Patterns Notes 1

Now, let's explore how to write data to a file in a more automated, user-friendly way using the try-with-resources statement in Java 7 and later.

Using try-with-resources to Write to a File

The try-with-resources statement is a handy feature in Java that automatically closes any object that implements the AutoCloseable interface, such as FileWriter. Here's how you can use it to write to a file:

Java Cheat Sheet: Operators, Scanner & User Input ☕
Java Cheat Sheet: Operators, Scanner & User Input ☕

try (FileWriter writer = new FileWriter("filename.txt")) { writer.write("Hello, file!"); }

Writing Multiple Lines with try-with-resources

☕ Java Basics Cheat Sheet for Beginners | Learn Java in One Page 🚀
☕ Java Basics Cheat Sheet for Beginners | Learn Java in One Page 🚀
Java Project Ideas For Beginners
Java Project Ideas For Beginners
the 50 java project ideas final year list is shown in orange and white, with text
the 50 java project ideas final year list is shown in orange and white, with text
a white poster with orange and black text that says, java projects create a password generator create an online survey system create
a white poster with orange and black text that says, java projects create a password generator create an online survey system create
java array operations
java array operations
Java Collection Framework: ArrayList Explained Visually
Java Collection Framework: ArrayList Explained Visually
an image of a computer screen with many lines on the screen and text below it
an image of a computer screen with many lines on the screen and text below it
Handwritten Notes || Java || Programming || Coding || BeingPro
Handwritten Notes || Java || Programming || Coding || BeingPro
Java Project Ideas For Practice 🔥🚀
Java Project Ideas For Practice 🔥🚀
Java 8 - How to count words in a text file in Java using NIO and Streams API
Java 8 - How to count words in a text file in Java using NIO and Streams API
Top Features of Java Every Beginner Should Know ☕🚀
Top Features of Java Every Beginner Should Know ☕🚀
Java User Input Notes | Scanner Class & Input Functions
Java User Input Notes | Scanner Class & Input Functions
Reading and writing data using file input stream and output stream class
Reading and writing data using file input stream and output stream class
Java OOP concepts cheat sheet
Java OOP concepts cheat sheet
Best Practices For Writing Secure Java Code In 2023
Best Practices For Writing Secure Java Code In 2023
Operators Summary in Java ☕
Operators Summary in Java ☕
http://chatgpt.com/g/g-lvtjtj09i-darkhorse-dns
http://chatgpt.com/g/g-lvtjtj09i-darkhorse-dns
Veil Lifted! Prime Python, Java, JavaScript Expertise: Mystery Solved – Order for Clear Victories!
Veil Lifted! Prime Python, Java, JavaScript Expertise: Mystery Solved – Order for Clear Victories!
JavaScript Basics You MUST Know
JavaScript Basics You MUST Know

Just like in the previous example, you can write multiple lines using the println method provided by the PrintWriter class.

try (PrintWriter writer = new PrintWriter(new FileWriter("filename.txt"))) { writer.println("Line 1"); writer.println("Line 2"); }

Appending to an Existing File with try-with-resources

You can also append data to an existing file using the try-with-resources statement by setting the second parameter of the FileWriter constructor to true.

try (FileWriter writer = new FileWriter("filename.txt", true)) { writer.write("Hello, file!"); }

In conclusion, writing to a file in Java is straightforward once you understand the basics of file handling and the various classes at your disposal. With the examples provided here, you should be equipped to handle most file writing scenarios in your Java projects.

However, file handling isn't just about writing data. Always remember to consider exceptions that might occur, such as the file not being found, or write permissions being denied. To learn more about these exceptions and how to handle them, check out our next tutorial on Java Exceptions.