Java 8+ version:

import java.nio.file.Paths;
import java.nio.file.Files;

Files.createDirectories(Paths.get("/Your/Path/Here"));

The Files.createDirectories() creates a new directory and parent directories that do not exist. This method does not throw an exception if the directory already exists.

Answer from Inês Gomes on Stack Overflow
🌐
Baeldung
baeldung.com › home › java › java io › create a directory in java
Create a Directory in Java | Baeldung
January 8, 2026 - As the new_directory doesn’t exist mkdir doesn’t create the underlying nested_directory. However, the File class provides us with another method to achieve that – mkdirs(). This method will behave like mkdir() but will also create all the unexisting parent directories as well. In our previous example, this would mean creating not only nested_directory, but also new_directory.
Discussions

file.mkdirs() not creating a directory
Since your file testing.txt in C:/Users/Me/Desktop/java/ already exists (System.out.println(f.exists()); shows true) you cannot create a directory with the same name. Further: Directories (folders) normally do not have any extensions. You are trying to create a directory named testing.txt on the desktop. More on reddit.com
🌐 r/javahelp
2
1
May 5, 2019
autocmd - How do I save a file in a directory that does not yet exist? - Vi and Vim Stack Exchange
Suppose I start Vim to edit a new file in a directory that is not yet created: vim nonExisitingDirectory/newFile.txt Vim will happily show me an empty buffer and I can start writing my new file. But More on vi.stackexchange.com
🌐 vi.stackexchange.com
February 10, 2015
How to create directory, if not exists
You are missing extra parentheses around the form in let. It should be (let ((var1 val1)) ) Additionally, if you set the second arg to non-nil, you can call make-directory directly without checking if the directory exists or not. (let ((projectile-dir (concat hanjaelee/emacs-temporal-directory "projectile/"))) (make-directory projectile-dir :parents)) More on reddit.com
🌐 r/emacs
4
4
July 6, 2016
Create directory if it does not exist

Take a look at python's os library. They have useful functions in manipulating files and folders.

I would look specifically at os.path.isdir(). This will allow your computer to detect if the path you pass into the method exists and is a directory. To create folders you can call os.mkdir(). I also suggest using full paths if you run into any trouble with path names.

More on reddit.com
🌐 r/learnpython
5
4
February 21, 2018
🌐
Coderanch
coderanch.com › t › 708245 › java › Creating-directory-doesn-exist-writing
Creating a directory if it doesn't exist and then writing a file (I/O and Streams forum at Coderanch)
If you create a File object for the directory you want to create you can use the isDirectory() and exists() methods to determine if a directory (or file) with that name already exists and if it doesn't use the mkDir() or mkDirs() method to create the directory.
🌐
Medium
medium.com › @AlexanderObregon › javas-files-createdirectories-method-explained-cb824678b0b7
Java’s Files.createDirectories() Method Explained | Medium
January 26, 2025 - If the directory doesn’t exist, it creates it along with any nonexistent parent directories. If the path points to a file (not a directory), it throws an exception.
🌐
Jenkov
jenkov.com › tutorials › java-io › file.html
Java File
December 17, 2019 - You can use the Java File class to create directories if they don't already exists. The File class contains the method mkdir() and mkdirs() for that purpose. The mkdir() method creates a single directory if it does not already exist.
🌐
GeeksforGeeks
geeksforgeeks.org › java › how-to-create-a-directory-in-java
How to Create a Directory in Java? - GeeksforGeeks
July 23, 2025 - // Java Program to Create Current Directory import java.io.File; // Driver Class public class GFG { // main function public static void main(String[] args) { // Sprecify the Directory Name String directoryName = "My_Learning"; // Address of Current Directory String currentDirectory = System.getProperty("user.dir"); // Specify the path of the directory to be created String directoryPath = currentDirectory + File.separator + directoryName; // Create a File object representing the directory File directory = new File(directoryPath); // Attempt to create the directory boolean directoryCreated = directory.mkdir(); if (directoryCreated) { System.out.println("Directory created successfully at: " + directoryPath); } else { System.out.println("Failed to create directory. It may already exist at: " + directoryPath); } } }
Find elsewhere
🌐
GitHub
gist.github.com › barbietunnie › a9b63e58d806859bba816862b8f101b3
Create recursive directory using Java NIO · GitHub
Create recursive directory using Java NIO. GitHub Gist: instantly share code, notes, and snippets.
🌐
Oracle
docs.oracle.com › javase › tutorial › essential › io › dirs.html
Creating and Reading Directories (The Java™ Tutorials > Essential Java Classes > Basic I/O)
You can create a temporary directory using one of createTempDirectory methods: createTempDirectory(Path, String, FileAttribute<?>...) ... The first method allows the code to specify a location for the temporary directory and the second method creates a new directory in the default temporary-file ...
🌐
Warp
warp.dev › terminus by warp › linux › mkdir if not exists
mkdir if not exists bash / linux | Warp
February 1, 2024 - Using mkdir -p is the easiest way to create directories when they don’t already exist. It can be elegantly combined with cp to quickly move files and directorie
🌐
How to do in Java
howtodoinjava.com › home › i/o › creating new directories in java
Creating New Directories in Java
April 21, 2022 - Notice the data is the parent directory of archive. In runtime, it is possible that data directory may not exist when we try to create archive directory. We will learn to create the archive directory in such a way: ... The createDirectory() creates the new directory if all the parent directories exist.
🌐
Reddit
reddit.com › r/javahelp › file.mkdirs() not creating a directory
r/javahelp on Reddit: file.mkdirs() not creating a directory
May 5, 2019 -

I've been trying to write a simple program that writes some text into a text file, but I haven't been able to create a directory for the files. This is what I have so far:

import java.io.*;

public class WritingTest3

{

public static void main(String[] args)

{

File f = null;

boolean bool = false;

try {

f = new File("C:/Users/Me/Desktop/java/testing.txt");

System.out.println(f.exists());

bool = f.mkdirs();

System.out.print("Directory created?" + bool);

} catch(Exception e) {

e.printStackTrace();

}

}

}

For checking whether the file exists, it is returning true, but when creating the directory it returns false. Thank you in advance.

🌐
W3Docs
w3docs.com › java
Create a directory if it does not exist and then create the files in that directory as well
To create a file in the directory, you can use the java.nio.file.Files class and its createFile method. Here is an example of how you can do this: import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; public class Main { public static void main(String[] args) throws Exception { String dirName = "mydir"; Path dirPath = Paths.get(dirName); if (!Files.exists(dirPath)) { Files.createDirectory(dirPath); } String fileName = "myfile.txt"; Path filePath = dirPath.resolve(fileName); if (!Files.exists(filePath)) { Files.createFile(filePath); } } }
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › nio › file › Files.html
Files (Java Platform SE 8 )
April 21, 2026 - NotDirectoryException - if the file could not otherwise be opened because it is not a directory (optional specific exception) ... SecurityException - In the case of the default provider, and a security manager is installed, the checkRead method is invoked to check read access to the directory. public static Path createFile(Path path, FileAttribute<?>... attrs) throws IOException · Creates a new and empty file, failing if the file already exists.
Top answer
1 of 7
50

As far as I know there is no setting or some such to do this. But not all is lost, we can of course use the BufWritePre autocommand.

This is executed before the buffer is written to the disk. So we can create the directory there if it doesn't exist yet.

For example:

augroup Mkdir
  autocmd!
  autocmd BufWritePre * call mkdir(expand("<afile>:p:h"), "p")
augroup END
  • <afile> refers to the file we're trying to save; :p is a modifier to expand it to the full pathname (rather than relative), and :h removes the last path component (the file).
  • We then call mkdir() if required. We need the p flag for mkdir() to make all parents directories (i.e. in the case of nonexistent/more_nonexisting/file), which also ensures it won't error out if the directory already exists.

You could, of course, also run the mkdir() command from the Vim commandline, or bind it to a keybind, ie:

nnoremap <Leader>m :call mkdir(expand("%:p:h"), "p")<CR>

Here I used % instead of <afile>, since that's only valid from within an autocommand (% refers to the currently active buffer, which would not work with :wa for example; <afile> refers to the filename of the buffer that triggers the autocmd).

You can also ask for a confirmation before writing a directory if you want. See this question for more details: How can I stop Vim from writing a file in BufWritePre autocommand?


The above snippet will create the directory on the first write (:w). You could, if you wanted, also create the directory when you first open it (i.e. just after typing vim ...) by using the BufNewFile autocmd instead of BufWritePre.


There is also a plugin called auto_mkdir which is effectively the same a the above.

On this page there is a slightly expanded snippet which also asks you if you want to create the directory first, which some may consider to be useful. It also has converts the filename of the encoding before writing it:

call mkdir(iconv(expand("%:p:h"), &encoding, &termencoding), 'p')

I'm not sure if this is actually required though, but if you mix encodings a lot and get weird filenames, you could try it.


I put all of the above in an auto_mkdir2.vim plugin for easier installation.

2 of 7
22

Another way with a vanilla Vim (without extra conf or plugins). in Vim:

:!mkdir -p /folder/you/want/
:w  #save file

or

$ vim /folder/you/want/myfile.conf
$ ctrl+z # switch to the terminal then create the folders
$ mkdir -p /folder/you/want/
$ fg # return to Vim
$ :w  #save file
🌐
OneCompiler
onecompiler.com › questions › 3t9vub5fz › making-sure-a-directory-with-given-path-exists-in-java
Making sure a directory with given path exists in Java - Questions - OneCompiler
public static void ensureDirectory(String dirPath) { if (dirPath != null && !dirPath.isEmpty()) { File dir = new File(dirPath); if (!dir.exists()) { Path newDirPath = Paths.get(dirPath); if (!Files.exists(newDirPath)) { try { Files.createDirectories(newDirPath); } catch (IOException e) { throw new RuntimeException("Exception while creating directory: " + dirPath, e); } } } } } Following is a sample program which tests the above function · import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; public class Test { publ
🌐
W3Schools
w3schools.com › java › java_files_create.asp
Java Create Files
In Java, you can create a new file with the createNewFile() method from the File class. ... Note that the method is enclosed in a try...catch block. This is necessary because it throws an IOException if an error occurs (if the file cannot be ...
🌐
O'Reilly
oreilly.com › library › view › java-cookbook › 0596001703 › ch10s10.html
Making New Directories - Java Cookbook [Book]
June 21, 2001 - Use java.io.File’s mkdir( ) or mkdirs( ) method. Of the two methods used for creating directories, mkdir( ) creates just one directory while mkdirs( ) creates any parent directories that are needed.
Author   Ian F. Darwin
Published   2001
Pages   888
🌐
TutorialsPoint
tutorialspoint.com › how-can-i-create-a-directory-if-it-does-not-exist-using-python
How can I create a directory if it does not exist using Python?
May 28, 2025 - Directory creation is a common task for computer users. You might need to create a directory to store some files, or to place some new files inside an existing directory. In this article, you will learn how to find if a directory already exists in Python or not.