Files and Directories

     

The first thing to note when dealing with files and directories in Java is that both files and directories are objects of type file. Don't look for java.io.Directory, cuz it doesn't exist.

The other confusing thing about working with files in Java is that when you create a new object of type java.io.File, you do not actually create a physical file on the file system. You have only created an object that can be used as a file. There are many classes and methods useful for doing this, which we'll look at in a moment, after we get used to dealing with File objects.

Fields

There are a number of fields in the java.io.File class that are useful when working with files and directories:

  • static String pathSeparator The system-dependent path -separator character, represented as a string for convenience. It's : on Unix and ; on Windows. This corresponds to System.getProperty("path.separator");.

  • static char pathSeparatorChar The system-dependent path-separator as a character.

  • static String separator The system-dependent default name -separator character, represented as a string for convenience. This is \ on Windows and / on UNIX.

  • static char separatorChar The system-dependent default name-separator character.

The following command gets the location in the file system from where the java command was invoked:

 

 String pwd = System.getProperty("user.dir"); 

Creating a Directory

Using the mkdir() method will create a directory for you. You must use the mkdirs() method (with an "s") if you need to create all non-existing ancestor directories automatically.

 

 //create dir without ancestor dirs     boolean isCreated = (new File("directoryName")).mkdir();     if (!isCreated) {         // dir not get created     } 

Note that this manner of creating a directory does not create any necessary ancestor directories. That is, if you try to specify a directory like /usr/eben/downloads/tomcat/ and the downloads directory does not exist, the mkdir method will not create it for you, even though it is a requirement for creating the tomcat directory. So it does nothing but return false.

The following code shows how to remedy that situation:

 

 //create dir and all ancestor dirs    boolean isCreated = (new File("directoryName")).  mkdirs()  ;    if (!isCreated) {        //dir not created } 

Deleting a Directory

Deleting an empty directory is done with the delete() method called on the File object representing the directory you want to delete. It returns a boolean indicating whether the directory could be deleted.

 

 //this will delete an empty directory boolean isDeleted = (new File("directoryName")).delete();     if (! isDeleted) {         // could not delete dir     } 

If there are files or directories in the directory you want to delete, you must recursively delete each one.

 

 /**  * Deletes a directory recursively. If the directory  * isn't empty, you have to first delete all  * subdirectories and files underneath it.  * <p>  * Note that this guy takes a file object instead of  * String because he calls himself (that's the  * recursive part). It prints out what it is doing  * as it does it.  * <p>  * Just call <code>new File("dirName")).delete();</code>  * if you know it is empty.  * @param directory The directory to delete.  */ public static boolean deleteDir(File directory){       if (directory.isDirectory()) {             //get array of strings of contained             //files and directories             String[] subdirs = directory.list();             //loop over entire array             for (int i=0; i < subdirs.length; i++) {               //call this same method, but create the               //instance with the name of the subdir                   File f = new File(directory,                   subdirs[i]);                   deleteDir(f);                   log("Deleted " + f);             }       } } 



Java Garage
Java Garage
ISBN: 0321246233
EAN: 2147483647
Year: 2006
Pages: 228
Authors: Eben Hewitt

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