Sub topic 6
Controlling How Files Are Opened
This chapter assumes basic Java syntax, familiarity with Path, Files, and checked exceptions, and Java 11 or later. The Files methods choose sensible defaults, but file-writing behavior changes when you provide StandardOpenOption values.
The default write behavior
When you call Files.writeString without open options, Java opens the file for writing, creates it if needed, and replaces its existing contents.
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
public class DefaultWrite {
public static void main(String[] args) throws IOException {
Path file = Path.of("notes.txt");
Files.writeString(file, "new contents\n");
System.out.println(Files.readString(file));
}
}The default options are effectively:
WRITE: open the file for writing.CREATE: create the file if it does not exist.TRUNCATE_EXISTING: remove existing contents before writing.
Running the program again replaces the previous contents.
Choosing `StandardOpenOption` values
StandardOpenOption is an enum containing common instructions for opening files. You can pass one or more values to methods such as Files.writeString.
| Option | Effect |
|---|---|
| `CREATE` | Create the file if it does not exist |
| `CREATE_NEW` | Create a new file and fail if it already exists |
| `APPEND` | Write after the existing contents |
| `TRUNCATE_EXISTING` | Remove existing contents before writing |
| `WRITE` | Open the file for writing |
To append, combine CREATE and APPEND:
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
public class AppendDemo {
public static void main(String[] args) throws IOException {
Path log = Path.of("app.log");
Files.writeString(
log,
"started\n",
StandardOpenOption.CREATE,
StandardOpenOption.APPEND);
}
}CREATE handles the first run. APPEND keeps existing text and adds the new text at the end.
Use CREATE_NEW when overwriting an existing file would be dangerous:
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
public class CreateOnce {
public static void main(String[] args) throws IOException {
Path file = Path.of("first-run.txt");
Files.writeString(
file,
"Created once\n",
StandardOpenOption.CREATE_NEW,
StandardOpenOption.WRITE);
}
}This program throws FileAlreadyExistsException if first-run.txt already exists. That failure lets the program detect an accidental duplicate instead of silently replacing data.
Common errors
APPEND does not mean “create if absent” on every API or file system. Pair it with CREATE when the file may not exist. TRUNCATE_EXISTING removes data immediately when the file is opened, so use it only when replacement is intended. All these operations can throw IOException, and CREATE_NEW can throw the more specific FileAlreadyExistsException.
Exercise
Which options should you use to add one line to a log while creating the log if it does not exist?
Show answer
Use StandardOpenOption.CREATE and StandardOpenOption.APPEND.