0

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.

MNJ
  • 172
  • 1
  • 1
  • 13
  • 3
    `f.mkdirs()` never makes files, only folders. – Mark Jeronimus Feb 03 '17 at 08:06
  • Or only `f.mkdir()`. – IQV Feb 03 '17 at 08:07
  • @MarkJeronimus File f=new File(path); this line makes it and also creates the directories, I use java 8 and Intellij – MNJ Feb 03 '17 at 08:08
  • No it doesn't. That just creates a `File` in memory. `mkdirs()` creates the directory. – user207421 Feb 03 '17 at 08:13
  • @EJP It should, but I debug it and on this line i get the folders and file, and on f.exists() i get true, tested 10 times now ... – MNJ Feb 03 '17 at 08:17
  • @MilosNikolik I do not understand you. `new File(...)` creates an object in memory, not a file in the file system. `mkdirs()` creates directories. Your problem is that it *did* create a directory, when what you wanted was a file, and the *parent* directories, which is addressed in my answer. The code in your edit doesn't make sense: you're calling `File.exists()` at a point where it *must* exist, otherwise an exception would have been thrown, and you are then throwing away the return value. – user207421 Feb 03 '17 at 09:05
  • @EJP everything you say is correct, after your answer i don't check existance. However my problem was not code related, but intellij, i had many watches that could of created the files and folders while i was debugging, or could be some other flow with the program. After restart of intellij and clearing of watches in debug it works as intended. Thank you for answer and comments! – MNJ Feb 03 '17 at 09:25

2 Answers2

3

It should be

f.getParentFile().mkdirs();

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

user207421
  • 305,947
  • 44
  • 307
  • 483
  • Note: If your file path does not name a parent directory, `getParentFile()` and `getParent()` will return null, which may cause a NullPointerException if the calling code does not handle it. – npace Feb 03 '17 at 08:14
  • @EJP it was intellij problem but i accept your answer, because it answers the title. – MNJ Feb 03 '17 at 08:46
0
File dir = new File("<Your_Path>/TestDirectory");

    // attempt to create the directory here
    boolean successful = dir.mkdir();
    if (successful)
    {
      // creating the directory succeeded
      System.out.println("directory was created successfully");
    }
    else
    {
      // creating the directory failed
      System.out.println("failed trying to create the directory");
    }

You can create your files inside your directory path from then on....

User_Targaryen
  • 4,125
  • 4
  • 30
  • 51