Java Write Files

Writing to a file lets your program save text that lasts. Java gives you FileWriter for the classic approach and Files.writeString for a modern one-liner. You will also learn the difference between overwriting a file and appending to it.

Writing means sending characters from your program into a file on disk. The most common classic tool is FileWriter, which opens a file for writing and lets you push text into it. When you are done you must close the writer so the data is flushed and the file is released. Modern Java makes this even shorter with a single Files method.

Writing text with FileWriter

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

public class Main {
  public static void main(String[] args) {
    try {
      FileWriter writer = new FileWriter("story.txt");
      writer.write("Once upon a time,\n");
      writer.write("a program learned to write files.");
      writer.close(); // important: flushes and releases the file
      System.out.println("Wrote to the file successfully.");
    } catch (IOException e) {
      System.out.println("Could not write: " + e.getMessage());
    }
  }
}
Note: By default, new FileWriter("story.txt") erases any existing contents of the file before writing. If the file had text in it, that text is gone. Use append mode when you want to add to a file instead of replacing it.

Appending instead of overwriting

Pass a second argument of true to the FileWriter constructor to open the file in append mode. New text is then added to the end of the file, leaving what was already there untouched. This is useful for log files where each run should add lines rather than wipe the history.

Appending to a file

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

public class Main {
  public static void main(String[] args) throws IOException {
    // The second argument, true, means append
    try (FileWriter writer = new FileWriter("log.txt", true)) {
      writer.write("New entry added at runtime.\n");
    }
    System.out.println("Entry appended.");
  }
}

Notice the try-with-resources form: try (FileWriter writer = ...). The writer is declared inside the parentheses, and Java closes it automatically at the end of the block, even if an exception is thrown. This is cleaner and safer than remembering to call close() yourself.

The modern one-liner

Writing with Files.writeString

import java.nio.file.Path;
import java.nio.file.Files;
import java.io.IOException;

public class Main {
  public static void main(String[] args) throws IOException {
    Path path = Path.of("quote.txt");
    Files.writeString(path, "Simple is better than complex.");
    System.out.println("Saved with one line.");
  }
}
ApproachOverwriteAppend
FileWriternew FileWriter(name)new FileWriter(name, true)
Files (nio)Files.writeString(path, text)Files.writeString(path, text, StandardOpenOption.APPEND)
  • FileWriter writes characters to a file and must be closed when finished.
  • By default writing overwrites; pass true as the second argument to append.
  • try-with-resources closes the writer for you automatically.
  • Files.writeString writes an entire string in one line.