When using
file.createNewFile();
I get the following exception
java.io.IOException: Parent directory of file does not exis
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); }