119

When using

file.createNewFile();

I get the following exception

java.io.IOException: Parent directory of file does not exist: /.../pkg/databases/mydb

I am wondering is there a createNewFile that creates the missing parent directories?

Mahozad
  • 18,032
  • 13
  • 118
  • 133
Pentium10
  • 204,586
  • 122
  • 423
  • 502

4 Answers4

177

Have you tried this?

file.getParentFile().mkdirs();
file.createNewFile();

I don't know of a single method call that will do this, but it's pretty easy as two statements.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • This only works if the file was created with a path string which contains a parent directory, i.e. `new File("file.txt").getParentFile()` returns `null`, `new File("dir/file.txt").getParentFile()` returns the same as `new File("dir")` – Zoltán Nov 21 '14 at 09:38
  • 1
    True, you don't need `mkdirs` if the file you are trying to create is not in a non-existent directory, but my use case is that I am creating multiple files, _some_ of which have parent directories, while others do not. – Zoltán Nov 21 '14 at 09:42
  • If your `File` instance is itself intended to be a folder, you can simply do: `folder.mkdirs();`. – zr0gravity7 Feb 08 '22 at 20:27
24

As of java7, you can also use NIO2 API:

void createFile() throws IOException {
    Path fp = Paths.get("dir1/dir2/newfile.txt");
    Files.createDirectories(fp.getParent());
    Files.createFile(fp);
}
Ted
  • 1,481
  • 1
  • 17
  • 18
20

Jon's answer works if you are certain that the path string with which you are creating a file includes parent directories, i.e. if you are certain that the path is of the form <parent-dir>/<file-name>.

If it does not, i.e. it is a relative path of the form <file-name>, then getParentFile() will return null.

E.g.

File f = new File("dir/text.txt");
f.getParentFile().mkdirs();     // works fine because the path includes a parent directory.

File f = new File("text.txt");
f.getParentFile().mkdirs();     // throws NullPointerException because the parent file is unknown, i.e. `null`.

So if your file path may or may not include parent directories, you are safer with the following code:

File f = new File(filename);
if (f.getParentFile() != null) {
  f.getParentFile().mkdirs();
}
f.createNewFile();
Zoltán
  • 21,321
  • 14
  • 93
  • 134
  • for a lot of reasons you should always include a path. How else should the system know where you want to put that file ? – NikkyD Sep 09 '15 at 15:21
  • @NikkyD I'm sorry, I don't quite understand your comment. In my answer, I'm not saying you're not including a path, but that the path passed may not include _parent directories_. There is also an example for such a path in the answer. – Zoltán Sep 09 '15 at 15:26
0

Searching for a Kotlin solution this generally-titled question pops up.
So, for Kotlin 1.9.0 and newer there is a new createParentDirectories() method on Path class:

Path("my/path/to/file.txt").createParentDirectories()
Mahozad
  • 18,032
  • 13
  • 118
  • 133