Java Files  «Prev  Next»


Lesson 9 Creating directories and paths
ObjectiveFamiliarize yourself with the methods used to create directories.

Working with Directories in Java

A File object can represent a directory as easily as a file. Most of the File methods, like
  1. getName(),
  2. canWrite(), and
  3. getPath(),
behave exactly the same for a directory as they do for a file. However, there are a couple of methods in the File class that behave differently when they operate on directories than they do when operating on ordinary files.
The delete() method only works on empty directories. If a directory contains even one file, it can't be easily deleted. If you attempt to delete a nonempty directory, delete() fails and returns false and no exception is thrown. The renameTo() method works on both empty and nonempty directories. However, whether a directory is empty or not the renameTo() method can only rename it, not move it to a different directory. If you attempt to move a directory into another directory, renameTo() fails and returns false and no exception is thrown. The File class also has several methods that just work with directories, not with regular files.

Creating directories

To create a file, you open a FileOutputStream to it (Java 1.0 and 1.1) or call createNewFile() (Java 2). This does not work for directories, though. For that purpose, the File class has a mkdir() method:

public boolean mkdir()

The mkdir() method attempts to create a directory with the path specified in the path field. If the directory is created, the method returns true. For example
File f = new File("tmp/");
f.mkdir();

Java I/O
The trailing slash is optional, but it helps you to remember that you are dealing with a directory rather than a plain file. If the security manager does not allow the directory to be created, a security exception is thrown. If the directory cannot be created for any other reason, mkdir() returns false. The mkdir() method only works for single directories. Trying to create a directory like com/macfaq/io/ with mkdir() only works if com/macfaq already exists.
The mkdirs() method creates every directory in a path that does not already exist:
public boolean mkdirs()

To actually create a file, you open a FileOutputStream to it. This will not work for directories, though. For that purpose the File class has a mkdir() method. For example, here is how you would create a directory named data:

File f = new File("data");
f.mkdir();

The mkdir() method tries to create a directory with the given name. If the directory is created, the method returns true, otherwise it returns false.
You can also create several directories at once. The mkdirs() method is one I have desired from other languages since I have been programming. Given a File object, the mkdirs() method creates not just one but every directory in the path as necessary, as long as the directories in the path have permissions.
To create the directory structure
java/awt/image in /usr/tmp

you would write:

File f = new File("/usr/tmp/java/awt/image");
f.mkdirs();

This would create the nested directories java, awt, and image inside the pre-existing directory
/usr/tmp.
The mkdirs() method returns true if all directories in this path are created and false if only some or none of them are created.
You may need to manually test the existence of each directory in the path if the method returns false, because it could have been partially successful.