0

I want to create a directory path in Java using the File#mkdirs() method. Let's consider the path "example/example/example". new File("example/example/example").mkdirs() creates the directories. However, if new File("example/example").exists() && new File("example/example").isFile() invoking new File("example/example/example").mkdirs() does nothing and returns false. If (and only if) this case happens, I want that the file gets deleted and the directory path is created instead. Is there any solution to this problem or do I have to handle it by myself (iterating through the path)?

stonar96
  • 1,359
  • 2
  • 11
  • 39
  • 1
  • 1
    If you’re asking whether there is a single method that will obliterate an existing file and replacing it with a directory, the answer is no. You will need to move or remove it yourself. – VGR Mar 05 '19 at 20:57
  • 1
    And you still probably shouldn't do it. – chrylis -cautiouslyoptimistic- Mar 05 '19 at 21:35
  • @chrylis I think I probably know what you mean but can you please explain it a bit more in detail what exactly I shouldn't do and why? Also note that if someone creates a correct answer, stating that it's not possible with a single method, providing an example code how it would be possible and explaining why I shouldn't do it or why it's bad, I would accept the answer. – stonar96 Mar 06 '19 at 00:59
  • 1
    Just going around deleting files is very likely to erase something important. It's much more usual to fail with an error message and let the user do that. – chrylis -cautiouslyoptimistic- Mar 06 '19 at 01:01

0 Answers0