javafilemkdirmkdirs

Java make directory but not file


So with the code below it creates the file in the folder

  File f = new File(path);
    if(!f.exists())
       f.mkdirs();

, but i only want to create the directory, because after this i use this code

file.transferTo(new File(path));

which saves a Multipart file to the same location, but it throws and error because there is already a file. Is there a way only to create the folder without the file ? One solution is to delete the first file, but looking for better solution

EDIT:

File f = new File(path);

this line creates the folders and the file, it shouldn't. I use java 8 and IntelliJ 14

SOLUTION:

The problem was Intellij or Intellij debug watches. After restarting it and clearing watches which were like:

new File(path) file.transferTo(new File(path)) f.exists() The code started working.


Solution

  • It should be

    f.getParentFile().mkdirs();
    

    You don't need to check for existence beforehand: mkdirs() already does that.