Friday, October 3, 2008

Java Gotcha: mkdir vs mkdirs

In Java's File class there is the mkdir and mkdirs methods but note that both of them work very differently.
  • mkdir: Creates the directory named by this abstract pathname.
  • mkdirs: Creates the directory named by this abstract pathname, including any necessary but nonexistent parent directories
So if you want to create a directory like /reports/customer with mkdir method it will be unable to create the /reports directory if it doesn't already exist.

This is one of those Java Gotchas that can waste alot of debugging time.

No comments: