๐ŸŽ“ A1Learner
๐Ÿ”
0 of 40 lessons completed

Java Writing Files

The Files class writes text to a file in one call. A Path value names the file the text goes to.

Write, then check the proof

The program writes one short text, then reads back two facts about the file. The size matches the byte count of the text.

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

public class Main {
  public static void main(String[] args) throws IOException {
    Path path = Path.of("note.txt");
    Files.writeString(path, "Stock check passed");
    System.out.println(Files.exists(path));
    System.out.println(Files.size(path));
  }
}

A list becomes lines

Each element of the list lands on its own line. The size counts every line ending as one byte.

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

public class Main {
  public static void main(String[] args) throws IOException {
    Path path = Path.of("items.txt");
    Files.write(path, List.of("pens", "books", "rulers"));
    System.out.println(Files.size(path));
  }
}

Append grows the file

The file starts with four bytes of text. The append call adds ten more bytes after them.

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

public class Main {
  public static void main(String[] args) throws IOException {
    Path path = Path.of("list.txt");
    Files.writeString(path, "pens");
    System.out.println(Files.size(path));
    Files.writeString(path, " and books", StandardOpenOption.APPEND);
    System.out.println(Files.size(path));
  }
}

Example

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.List;

public class Main {
  public static void main(String[] args) throws IOException {
    Path report = Path.of("report.txt");
    Files.writeString(report, "Daily stock report");
    System.out.println(Files.exists(report));
    System.out.println(Files.size(report));
    Path items = Path.of("items.txt");
    Files.write(items, List.of("apples", "pears"));
    System.out.println(Files.size(items));
    Files.writeString(report, " - final", StandardOpenOption.APPEND);
    System.out.println(Files.size(report));
  }
}

Key points

  • Path.of builds a Path from a file name text.
  • Files.writeString writes a whole string to the file in one call.
  • Writing to an existing file replaces its content by default.
  • Files.write with a list writes one line per element.
  • The APPEND option adds new text after the existing content instead of replacing it.
  • Files.size answers the file length in bytes, so a written text can be checked exactly.

In this example, one file receives a whole report, a second file receives a list of items, and an append call grows the report by eight bytes.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

What does the APPEND option do when a file already holds text?