Java Create New File using Files.createFile()

Tags: Files.createFile Java NIO

In this Java NIO tutorial we learn how to create a new file in Java application using the Files.createFile() method.

Table of contents

  1. How to use Files.createFile() method
  2. More Files.createFile() Java Examples

How to use Files.createFile() method

The Files.createFile() method use to create an empty file at given location, for example we have the Java program below to create a new empty file at D:\SimpleSolution\Data\Document.txt

FilesCreateFileExample1.java

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

public class FilesCreateFileExample1 {
    public static void main(String... args) throws IOException {
        Path fileToCreate = Paths.get("D:\\SimpleSolution\\Data\\Document.txt");

        Path createdFile = Files.createFile(fileToCreate);

        System.out.println("Successful create new file " + createdFile);
    }
}
The output as below.
Successful create new file D:\SimpleSolution\Data\Document.txt

If the file already existed the application throws java.nio.file.FileAlreadyExistsException exception as below.

Exception in thread "main" java.nio.file.FileAlreadyExistsException: D:\SimpleSolution\Data\Document.txt
	at sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:81)
	at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:97)
	at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:102)
	at sun.nio.fs.WindowsFileSystemProvider.newByteChannel(WindowsFileSystemProvider.java:230)
	at java.nio.file.Files.newByteChannel(Files.java:361)
	at java.nio.file.Files.createFile(Files.java:632)
	at FilesCreateFileExample1.main(FilesCreateFileExample1.java:10)

To avoid FileAlreadyExistsException exception above we can check file existing before creating new file using the Files.exists() method as following example Java program.

FilesCreateFileExample2.java

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

public class FilesCreateFileExample2 {
    public static void main(String... args) throws IOException {
        Path fileToCreate = Paths.get("D:\\SimpleSolution\\Data\\Document.txt");

        if(Files.exists(fileToCreate)) {
            System.out.println("The file " + fileToCreate + " already existed.");
        } else {
            Path createdFile = Files.createFile(fileToCreate);

            System.out.println("Successful create new file " + createdFile);
        }
    }
}
The output as below.
The file D:\SimpleSolution\Data\Document.txt already existed.

More Files.createFile() Java Examples

Create Empty File in Temporary Directory

In the following Java program we use the Files.createFile() method to create a new empty file in user’s temporary directory.

FilesCreateFileExample3.java

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

public class FilesCreateFileExample3 {
    public static void main(String... args) throws IOException {
        String temporaryDirectory = System.getProperty("java.io.tmpdir");
        Path fileToCreate = Paths.get(temporaryDirectory).resolve("SimpleSolution.txt");

        if(Files.exists(fileToCreate)) {
            System.out.println("The file " + fileToCreate + " already existed.");
        } else {
            Path createdFile = Files.createFile(fileToCreate);

            System.out.println("Successful create new file " + createdFile);
        }
    }
}
The output as below.
Successful create new file C:\Users\SS\AppData\Local\Temp\SimpleSolution.txt

Create and Write Text to File

In the following example Java program we use Files.createFile() method to create a new text file and write text to that new file.

FilesCreateFileExample4.java

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

public class FilesCreateFileExample4 {
    public static void main(String... args) throws IOException {
        Path fileToCreate = Paths.get("D:\\SimpleSolution\\Data\\Document.txt");

        if(!Files.exists(fileToCreate)) {
            Path createdFile = Files.createFile(fileToCreate);
            try(FileOutputStream outputStream = new FileOutputStream(createdFile.toFile())) {
                outputStream.write("Simple Solution".getBytes());

                System.out.println("Successfully create and write text to file " + createdFile);
            }
        }
    }
}
The output as below.
Successfully create and write text to file D:\SimpleSolution\Data\Document.txt

Happy Coding 😊

Java Create Parent and Sub Directories using Files.createDirectories()

Java Copy File using Files.copy()

Java Get All Entries in Directory using Files.newDirectoryStream()

Java Create a New Directory using Files.createDirectory()