In Java, Files.createFile(path) creates a new, empty file at path and returns that same path. If a file, folder, or link with that name already exists, it throws FileAlreadyExistsException and leaves the existing entry alone.
The check "does it exist?" and the creation happen as one atomic step, so two programs racing to create the same file cannot both succeed. That makes it useful for lock or marker files. The parent folder must already exist.
A static method of the final utility class java.nio.file.Files, available since Java 7. Call it on the class name, as in Files.createFile(...), after import java.nio.file.Files;. You never create a Files object.
Syntax
Pass the path to create; optional file attributes (such as POSIX permissions) can follow:
Also sets attributes atomically, e.g. POSIX permissions.
Files.write(p, bytes)
Creates or overwrites a file and fills it in one call.
Files.createDirectories(p.getParent())
Run first when the parent folder may be missing.
Since Java 7. Unlike java.io.File.createNewFile(), which returns false when the file exists, Files.createFile tells you why it failed through the exception type.
Library declarations
Java
public static Path createFile(Path path, FileAttribute<?>... attrs) throws IOException
Parameters
Parameter
Description
path
The file to create. Its parent directory must exist.
attrs
Optional FileAttribute values set atomically while creating, such as PosixFilePermissions.asFileAttribute(...). Usually omitted.
Return Value
Return Value
Description
Path
The path argument itself, now naming an empty file of size 0.
Run with java CreateFileNoOverwrite. The output is the same on Windows, macOS, and Linux.
Second call: FileAlreadyExistsException
Content still: keep me
How It Works
Fill the new file. After the first createFile, Files.write stores the 7 bytes of "keep me".
The name is taken. The second createFile sees the existing file and throws FileAlreadyExistsException, so the "Created again?" line is never printed.
Nothing was truncated. Reading the bytes back still gives "keep me", proving createFile refuses rather than replaces. This is the safe choice for marker or lock files.
Example 3 — Create the Parent Folders First
A file inside a folder that does not exist cannot be created. Catch the error, create the folders with createDirectories, and try again.
Java
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
public class CreateFileParents {
public static void main(String[] args) throws IOException {
Path dir = Files.createTempDirectory("demo");
Path file = dir.resolve("logs").resolve("app.log");
try {
Files.createFile(file);
} catch (NoSuchFileException e) {
System.out.println("First try: the logs folder is missing");
}
Files.createDirectories(file.getParent());
Files.createFile(file);
String rel = dir.relativize(file).toString().replace('\\', '/');
System.out.println("Second try created: " + rel);
System.out.println("Is a regular file: " + Files.isRegularFile(file));
Files.delete(file);
Files.delete(file.getParent());
Files.delete(dir);
}
}
Output
Run with java CreateFileParents. The output is the same on Windows, macOS, and Linux.
First try: the logs folder is missing
Second try created: logs/app.log
Is a regular file: true
How It Works
Two levels deep. The target is logs/app.log inside the temp folder, but no logs folder exists yet.
No automatic parents.createFile only creates the last name, so it throws NoSuchFileException on Windows, macOS, and Linux alike.
Fix and retry.createDirectories(file.getParent()) creates logs, and the second createFile succeeds.
Portable printing.relativize drops the random temp part and replace('\\', '/') turns Windows separators into slashes, so every system prints logs/app.log.
Frequently Asked Questions
It throws FileAlreadyExistsException and does not touch the existing file. It never overwrites or truncates.
No. If the parent folder does not exist the call fails, typically with NoSuchFileException. Call Files.createDirectories(path.getParent()) first.
Both create an empty file atomically. File.createNewFile returns false when the file exists, while Files.createFile throws FileAlreadyExistsException and returns the Path.
Yes. Checking that the file does not exist and creating it are a single atomic operation with respect to other file-system activity, so only one of several competing programs can succeed.
On Linux and macOS pass PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString("rw-------")) as the second argument. On Windows that attribute is not supported and the call throws UnsupportedOperationException.