Java Create Files
Ways to Create a File
Java offers several ways to create a file. They differ in one important way: what happens if the file already exists.
| Method | If the file already exists | Package |
|---|---|---|
Files.createFile(path) | Throws FileAlreadyExistsException | java.nio.file |
File.createNewFile() | Returns false | java.io |
Files.writeString(path, text) / Files.write(...) | Replaces its content (by default) | java.nio.file |
Files.writeString(path, text, CREATE_NEW, WRITE) | Throws FileAlreadyExistsException | java.nio.file |
Files.createTempFile(prefix, suffix) | Can't happen — it generates a new, unique name | java.nio.file |
Choosing the right one matters. Some code must never overwrite existing data (receipts, uploaded files, audit logs), while other code should simply produce a fresh output each time (a daily report). Creating an empty file explicitly is also useful as a marker or lock — for example a .done file that tells another process an export is complete.
Files.createFile
import java.io.IOException;
import java.nio.file.*;
public class Create1 {
public static void main(String[] args) throws IOException {
Path file = Path.of("orders.log");
Path created = Files.createFile(file);
System.out.println("created " + created + ", size " + Files.size(created));
try {
Files.createFile(file);
} catch (FileAlreadyExistsException e) {
System.out.println("FileAlreadyExistsException: " + e.getFile());
}
try {
Files.createFile(Path.of("archive/2026/orders.log"));
} catch (NoSuchFileException e) {
System.out.println("NoSuchFileException: " + e.getFile());
}
}
}
Output (when run in an empty folder):
created orders.log, size 0
FileAlreadyExistsException: orders.log
NoSuchFileException: archive/2026/orders.log
How it works:
Files.createFile(path)creates a new, empty file (size 0) and returns itsPath.- The check and the creation are one atomic operation. The operating system checks whether the name is free and creates the file in a single step, so no other program can create a file with the same name in between. That's why a second call fails with
FileAlreadyExistsExceptioninstead of silently succeeding or overwriting. - Parent folders are not created.
archive/2026doesn't exist, so the call throwsNoSuchFileException. ItsgetFile()reports the path that was being created. getFile()on these exceptions (both extendFileSystemException) returns the affected path as aString, which is handy for error messages.
File.createNewFile (java.io)
The older java.io.File API has an equivalent that reports the result as a boolean:
import java.io.*;
public class Create2 {
public static void main(String[] args) throws IOException {
File file = new File("invoice.txt");
System.out.println("first call: " + file.createNewFile());
System.out.println("second call: " + file.createNewFile());
try {
new File("no/such/dir/invoice.txt").createNewFile();
} catch (IOException e) {
System.out.println("IOException: " + e.getMessage());
}
}
}
Output (on Linux):
first call: true
second call: false
IOException: No such file or directory
truemeans the file was created;falsemeans a file with that name already existed. It is also atomic.- A missing parent folder throws
IOExceptionwith the operating system's message, which is less specific thanNoSuchFileExceptionand differs between operating systems (Windows reports "The system cannot find the path specified").
Ignoring the boolean is a common oversight: file.createNewFile(); on its own line silently does nothing when the file exists. You'll see this method in older code; for new code, Files.createFile gives clearer exceptions.
Creating the Parent Folders First
Files usually go into a folder structure that may not exist yet — for example one folder per year and month.
import java.io.IOException;
import java.nio.file.*;
public class Create3 {
public static void main(String[] args) throws IOException {
Path file = Path.of("archive", "2026", "11", "orders.log");
Files.createDirectories(file.getParent());
Files.createFile(file);
System.out.println("created " + file);
System.out.println("again: " + Files.createDirectories(file.getParent()));
try {
Files.createDirectory(Path.of("archive"));
} catch (FileAlreadyExistsException e) {
System.out.println("createDirectory: already exists " + e.getFile());
}
}
}
Output (on Linux; Windows prints