← Back to Core Java Course | Chapter 12: File I/O | Lesson 3 of 9

Java Create Files

A new file can be created on disk using either the classic File class's createNewFile method or the newer NIO Files.createFile method, both of which also support creating directories.

Creating a File with the File Class

A new empty file can be created on disk using the File class's createNewFile method, which returns true if the file was actually created and false if a file already existed at that path.

Example: Creating a File with the File Class

java
import java.io.File;
import java.io.IOException;
public class Main {
	public static void main(String[] args) throws IOException {
		File file = new File("output.txt");
		boolean created = file.createNewFile();
		System.out.println(created);
	}
}

Checking if a File Already Exists

Before creating a file, it's common to check whether it already exists using the exists method, since attempting to create a file that's already there simply does nothing rather than causing an error.

Example: Checking if a File Already Exists

java
import java.io.File;
import java.io.IOException;
public class Main {
	public static void main(String[] args) throws IOException {
		File file = new File("output.txt");
		if (!file.exists()) {
			file.createNewFile();
		}
		System.out.println(file.exists());
	}
}

Creating Directories

Directories are created separately from files using mkdir for a single new directory, which fails if its parent doesn't exist, or mkdirs for creating an entire missing chain of parent directories at once.

Example: Creating Directories

java
import java.io.File;
public class Main {
	public static void main(String[] args) {
		File dir = new File("data/backup"); // parent missing
		System.out.println(dir.mkdir()); // fails: parent doesn't exist
		System.out.println(dir.mkdirs()); // creates whole chain
	}
}

Creating Files with NIO

The newer NIO.2 API, built around Path and the Files utility class, offers createFile and createDirectories as a more modern alternative to the classic File class for creating files and directories.

Example: Creating Files with NIO

java
import java.nio.file.Files;
import java.nio.file.Path;
import java.io.IOException;
public class Main {
	public static void main(String[] args) throws IOException {
		Path path = Path.of("nio-output.txt");
		if (!Files.exists(path)) {
			Files.createFile(path);
		}
		System.out.println(Files.exists(path));
	}
}

Handling Creation Errors

File creation can fail for reasons like missing permissions or a full disk, and the two APIs report this differently: the classic File class returns false or throws IOException, while the NIO API throws a specific exception like FileAlreadyExistsException.

Example: Handling Creation Errors

java
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
public class Main {
	public static void main(String[] args) {
		try {
			Files.createFile(Path.of("/nonexistent-dir/file.txt")); // NIO throws
		} catch (IOException e) {
			System.out.println("NIO threw: " + e.getClass().getSimpleName());
		}
	}
}

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.