Java Create Files
In this page:
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
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);
}
}
Login to try C/C++/Java/PHP code in the editor
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
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());
}
}
Login to try C/C++/Java/PHP code in the editor
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
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
}
}
Login to try C/C++/Java/PHP code in the editor
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
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));
}
}
Login to try C/C++/Java/PHP code in the editor
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
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 try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: