Support Online
Skip to main content

Java FileWriter Guide: Examples of Writing Characters to File

📘 What Will You Learn in This Guide?

In this guide, you will learn how to write character-based data using the FileWriter class in Java's java.io package.
You will see step by step different write() methods, try-with-resources structure, flush() and close() differences and performance enhancing tips.

⚙️ What is FileWriter?

FileWriter is a subclass of class OutputStreamWriter and is used to write text (Unicode characters).
It works with 16-bit characters instead of raw bytes. In this way, it supports all languages ​​including Turkish.

💡 Tip:
For higher performance you can wrap the FileWriter with BufferedWriter:

BufferedWriter writer = new BufferedWriter(new FileWriter("tr1-veri/log.txt"));

🧩 Step 1: Creating a FileWriter Object

The FileWriter object can be created via various constructors:

FounderDescription
FileWriter(String fileName)It creates a new file and overwrites it if there is one.
FileWriter(String fileName, add boolean)Appends to the end of the file.
FileWriter(File file)Creates via File object.

🧩 Step 2: Secure Writing with Try-with-Resources

Since FileWriter implements the AutoCloseable interface, it closes automatically in the try-with-resources structure. This method prevents resource leaks.


import java.io.FileWriter;

public class TekKarakterYazma {
public static void main(String[] args) {
try (FileWriter dosyaYazici = new FileWriter("tr1-veri/log.txt")) {
dosyaYazici.write(65); // 'A'
dosyaYazici.write(66); // 'B'
dosyaYazici.write(67); // 'C'
} catch (Exception e) {
e.printStackTrace();
}
}
}

💬 Output: ABC


🧩 Step 3: Methods for Writing Strings and Arrays

🔹 Writing Entire String (write(String str))


try (FileWriter dosyaYazici = new FileWriter("tr1-veri/log.txt")) {
dosyaYazici.write("GenixNode Teknoloji");
}

💬 Released: GenixNode Technology

🔹 Writing a String Fragment (write(String str, int off, int len))


String veri = "Bu bir FileWriter Uygulamasıdır.";
try (FileWriter dosyaYazici = new FileWriter("tr1-veri/log.txt")) {
dosyaYazici.write(veri, 8, 10);
}

💬 Output: FileWriter


🧩 Step 4: Using flush() and close()


try (FileWriter dosyaYazici = new FileWriter("tr1-veri/log.txt")) {
dosyaYazici.write("İşlem Başladı");
dosyaYazici.flush(); // Veriyi hemen diske yazar
dosyaYazici.write(" - İşlem Tamamlandı");
}

💬 Description: flush() forcibly transfers the data in the buffer to disk. When the close() method is called, flush() automatically runs, then closes the stream.


⚔️ FileWriter vs FileOutputStream

FeatureFileWriterFileOutputStream
Data TypeCharacter (16-bit)Byte (8-bit)
Area of ​​UseText filesBinary files (image, audio, etc.)
Unicode Support✔️
PerformanceFaster with BufferedWriterOptimized for raw writing

💡 Performance Tip When working with large files, using BufferedWriter speeds up the writing process:


BufferedWriter bw = new BufferedWriter(new FileWriter("tr1-veri/log.txt"));
bw.write("GenixNode Test Logu");
bw.close();

❓ Frequently Asked Questions (FAQ)

  1. How to add data to the end of existing file?

Create it as new FileWriter("file.txt", true). This enables append mode.

2.What is the difference between flush() and close()?

flush() writes the data to disk, close() completely closes the stream.

  1. Can I create folders with FileWriter?

No. To create a folder new File("folder").mkdirs(); is used.

  1. Why is using BufferedWriter faster?

Because it accumulates data in memory and writes it to disk in bulk, reducing I/O operations.


🎯 Result

Now you know how to write character-based data using the FileWriter class in Java. You can use tools like try-with-resources and BufferedWriter to make your codes safe, fast and organized.

☁️ You can test the performance difference yourself by testing the file operations you have developed on GenixNode.