EMZETT.
Login

Create Files

In short: Creating a new, empty file in the file system — e.g. with File.createNewFile() or Files.createFile(path).

In more detail: createNewFile() returns false (instead of an exception) if the file already exists — easy to miss if you only check for exceptions. Files.createFile(path) from java.nio.file, by contrast, throws a FileAlreadyExistsException, which makes errors more likely to be noticed during testing.

In Depth

// Older API (java.io)
File file = new File("new.txt");
boolean created = file.createNewFile(); // false = already existed, NO exception
 
// Modern API (java.nio.file) - since Java 7
Path path = Path.of("new2.txt");
try {
    Files.createFile(path); // throws FileAlreadyExistsException if the file already exists
} catch (FileAlreadyExistsException e) {
    System.out.println("Already exists");
} catch (IOException e) {
    System.err.println("Error: " + e.getMessage());
}
 
// Creating directories (parent folders are created too if needed)
Files.createDirectories(Path.of("folder/subfolder"));

The newer java.nio.file API (Path/Files) is considered the better standard today compared to the older java.io.File class: more informative exceptions instead of silent boolean returns, better support for symbolic links and file attributes, and Files.createDirectories() creates the entire folder structure at once if needed (unlike mkdir(), which only creates a single folder and fails if the parent folder is missing).

Creating a file with content in one step

Often a file shouldn’t just be created, but immediately filled with content — Files.writeString() (since Java 11) is better suited for this than a separate createFile() + write():

Path path = Path.of("configuration.txt");
Files.writeString(path, "setting=value\n", StandardOpenOption.CREATE); // creates + writes in one step

Temporary files

For files only needed during program runtime (e.g. intermediate results), Java offers Files.createTempFile() — the operating system automatically picks a suitable temporary location:

Path temp = Files.createTempFile("processing", ".tmp");
temp.toFile().deleteOnExit(); // automatically deleted when the JVM exits

Catching permission errors

Besides “file already exists”, a missing write permission in the target directory is the second most common source of error — Files.createFile() throws an AccessDeniedException (a subclass of IOException) for this, which can be specifically caught to show the user a more understandable error message than a raw stack trace.

try-with-resources for file operations

Even though createFile() itself doesn’t leave a resource open, the write operations that follow directly almost always follow the try-with-resources pattern, so that file handles are reliably closed even if an exception occurs:

Path path = Files.createFile(Path.of("log.txt"));
try (BufferedWriter writer = Files.newBufferedWriter(path)) {
    writer.write("first entry");
} // writer is automatically closed, even on an exception

Without try-with-resources, closing would have to happen manually in a finally block — easy to forget, especially when several resources are open at the same time.

See also: Files, Write Files, Exceptions