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:

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.




JavaT Phrasebook. Essential Code and Commands
Java Phrasebook
ISBN: 0672329077
EAN: 2147483647
Year: 2004
Pages: 166

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net