Creating a New Directory

boolean success = (new File("users/tim")).mkdir();

In this phrase, we use the mkdir() method of the File class to create a new directory. This method will return true if the directory is successfully created. If the directory cannot be created, the method returns false. The mkdir() method will only create a directory if all specified parent directories already exist. In the preceding phrase, the users directory must already exist for this execution of mkdir() to successfully create the users/tim directory.

A similar method will allow you to create an entire directory tree, including all parent directories, if they do not exist. This is the mkdirs() method on the File class. Here, we show an example using this method: ...

Get Java™ Phrasebook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.