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: boolean success = (new File("/users/tim/web")).mkdirs( ); In the preceding example, the mkdirs() method will create any of the directories (users, tim, web) that do not exist. |