← 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.
Syntax
java
import java.io.File;

File file = new File("file_name");
file.createNewFile();

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 so it can be used by its short name
import java.io.File;
// Import java.io.IOException so it can be used by its short name
import java.io.IOException;
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) throws IOException {
		// Create a new File object and store it in file
		File file = new File("output.txt");
		// Declare created and set it to file.createNewFile()
		boolean created = file.createNewFile();
		// Print a line to the console
		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 so it can be used by its short name
import java.io.File;
// Import java.io.IOException so it can be used by its short name
import java.io.IOException;
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) throws IOException {
		// Create a new File object and store it in file
		File file = new File("output.txt");
		// Check whether !file.exists()
		if (!file.exists()) {
			file.createNewFile();
		}
		// Print a line to the console
		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 so it can be used by its short name
import java.nio.file.Files;
// Import java.nio.file.Path so it can be used by its short name
import java.nio.file.Path;
import java.io.IOException;
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) throws IOException {
		Path path = Path.of("nio-output.txt");
		// Check whether !Files.exists(path)
		if (!Files.exists(path)) {
			Files.createFile(path);
		}
		// Print a line to the console
		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());
		}
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Ignoring the result of createNewFile(), which returns false when the file already exists.
  2. Forgetting to handle IOException, which is a checked exception.
  3. Using mkdir() when parent folders are missing, when mkdirs() creates them.

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.