Directory Class

Used to represent a directory on a file system. In many ways, this class is an extension of the java.io.File class. However, unlike the java.io.File class, this object provides functions that are relevant and specific to directories. For example, this class provides a mechanism to move and copy directories - something not offered by the java.io.File class. In addition, this class provides a mechanism to retrieve files and folders found in a directory AND any subdirectories. This is accomplished via a multi-threaded recursive search. Finally, this class provides a powerful tool to monitor changes made to the directory (e.g. getEvents).

Constructors

Directory( String Path )
Directory( java.io.File File )

Properties

PathSeparator

Public Methods

exists( ) returns boolean
Used to determine whether a directory exists on the file system.
isEmpty( ) returns boolean
Used to determine whether the directory is empty. Returns true if no files or directories are present in the current directory.
create( ) returns boolean
Used to create the directory.
delete( ) returns boolean
Used to delete the directory.
copyTo( Directory Destination, boolean Overwrite ) returns String[]
Used to copy a directory to another directory. Preserves the last modified date associated with the source files and directories. Returns a list of any files that failed to copy.
copyTo( Directory Destination, Object filter, boolean Overwrite ) returns String[]
Used to copy a directory to another directory. Provides a filter option to copy specific files (e.g. "*.jpg"). Preserves the last modified date associated with the source files and directories. Returns a list of any files that failed to copy.
Destination
filterA file filter. You can pass in a java.io.FileFilter, a String (e.g. "*.txt"), or an array of Strings (e.g. String[]{"*.txt", "*.doc"}). Wildcard filters are supported. Note that the filter is only applied to files, not directories.
OverwriteIf true, overwrites any existing files/directories
moveTo( Directory Destination, boolean Overwrite ) returns void
Used to move a directory from one directory to another.
rename( String Name ) returns void
Used to rename the directory.
getName( ) returns String
Returns the name of the directory (excluding the path).
getPath( ) returns String
Returns the full path to this directory, including the directory name. The path includes a system-dependent default name-separator character at the end of the string (e.g. "/" or "\").
getDate( ) returns java.util.Date
Returns a timestamp of when the directory was last modified. This is identical to the getLastModifiedTime() method.
setDate( java.util.Date lastModified ) returns boolean
Used to set the timestamp of when the directory was last modified.
getSize( ) returns long
Returns the total size of all the files and folders found in this directory, in bytes.
isHidden( ) returns boolean
Used to determine whether this Directory is Hidden
isLink( ) returns boolean
Used to determine whether the directory is actually a link to another file or directory. Returns true for symbolic links and Windows junctions.
getLink( ) returns java.io.File
Returns the target of a symbolic link or Windows junction
getLastModifiedTime( ) returns java.util.Date
Returns a timestamp of when the directory was last modified. This is identical to the getDate() method.
getCreationTime( ) returns java.util.Date
Returns a timestamp of when the directory was first created. Returns a null if the timestamp is not available.
getLastAccessTime( ) returns java.util.Date
Returns a timestamp of when the directory was last accessed. Returns a null if the timestamp is not available.
getFlags( ) returns java.util.HashSet<String>
Returns keywords representing directory attributes. Returns an empty HashSet if the attributes are not available.
getFileAttributes( ) returns File.FileAttributes
Returns file attributes such as when the file was first created and when it was last accessed. File attributes are cached for up to one second. This provides users the ability to retrieve multiple attributes at once. Without caching, we would have to ping the file system every time we call getLastAccessTime(), getLastAccessTime(), getLastWriteTime(), etc. The cached attributes are automatically updated when the file is updated or deleted by this class.
getParentDirectory( ) returns Directory
Used to retrieve this Directory's Parent. Returns null if there is no parent directory.
toFile( ) returns java.io.File
Used to retrieve the java.io.File representation by this object.
getFiles( Object filter ) returns File[]
Used to retrieve an array of files found in this directory. Returns an empty array if no files are found.
filterA file filter. You can pass in a java.io.FileFilter, a String (e.g. "*.txt"), or an array of Strings (e.g. String[]{"*.txt", "*.doc"}). Wildcard filters are supported. Note that the filter is only applied to files, not directories.
getFiles( ) returns File[]
Used to retrieve an array of files found in this directory. Returns an empty array if no files are found.
getFiles( Object filter, boolean RecursiveSearch ) returns File[]
Used to retrieve an array of files found in this directory. Returns an empty array if no files are found.
filterA file filter. You can pass in a java.io.FileFilter, a String (e.g. "*.txt"), or an array of Strings (e.g. String[]{"*.txt", "*.doc"}). Wildcard filters are supported. Note that the filter is only applied to files, not directories.
RecursiveSearchIf true, will perform a multi-threaded, recursive directory search to find all the files found in the current directory, including any subdirectories. If false, the method will simply return files found in the current directory.
Note that if the thread is interrupted for whatever reason during a recursive search, the search will stop immediately. Consequently, the returned array may be incomplete. You can check the interrupted status with the Thread.isInterrupted() method. Alternatively, you can read and clear the interrupted status in a single operation using the Thread.interrupted() method.
getFiles( boolean RecursiveSearch ) returns File[]
Used to retrieve an array of files found in this directory.
RecursiveSearchIf true, will perform a multi-threaded, recursive directory search to find all the files found in the current directory, including any subdirectories. If false, the method will simply return files found in the current directory.
Note that if the thread is interrupted for whatever reason during a recursive search, the search will stop immediately. Consequently, the returned array may be incomplete. You can check the interrupted status with the Thread.isInterrupted() method. Alternatively, you can read and clear the interrupted status in a single operation using the Thread.interrupted() method.
getSubDirectories( ) returns Directory[]
Used to retrieve an array of directories found in this directory.
getSubDirectories( boolean RecursiveSearch ) returns Directory[]
Used to retrieve an array of directories found in this directory.
RecursiveSearchIf true, will perform a multi-threaded, recursive directory search to find all the directories found in the current directory, including any subdirectories. If false, the method will simply return directories found in the current directory.
Note that if the thread is interrupted for whatever reason during a recursive search, the search will stop immediately. Consequently, the returned array may be incomplete. You can check the interrupted status with the Thread.isInterrupted() method. Alternatively, you can read and clear the interrupted status in a single operation using the Thread.interrupted() method.
getChildren( ) returns List
Used to retrieve an array of both files and folders found in this directory.
getChildren( boolean RecursiveSearch ) returns List
Used to retrieve an array of both files and folders found in this directory.
getChildren( boolean RecursiveSearch, Object filter ) returns List
Used to retrieve an list of files and folders found in this directory.
RecursiveSearchIf true, will include files found in the subdirectories.
filterA file filter. You can pass in a java.io.FileFilter, a String (e.g. "*.txt"), or an array of Strings (e.g. String[]{"*.txt", "*.doc"}). Wildcard filters are supported. Note that the filter is only applied to files, not directories.
getChildren( boolean RecursiveSearch, Object filter, boolean wait ) returns List
Used to retrieve an list of files and folders found in this directory.
RecursiveSearchIf true, will perform a multi-threaded, recursive directory search to find all the files and folders found in the current directory, including any subdirectories. If false, the method will simply return items found in the current directory.
Note that if the thread is interrupted for whatever reason during a recursive search, the search will stop immediately. Consequently, the returned array may be incomplete. You can check the interrupted status with the Thread.isInterrupted() method. Alternatively, you can read and clear the interrupted status in a single operation using the Thread.interrupted() method.
filterA file filter. You can pass in a java.io.FileFilter, a String (e.g. "*.txt"), or an array of Strings (e.g. String[]{"*.txt", "*.doc"}). Wildcard filters are supported. Note that the filter is only applied to files, not directories.
waitUsed to indicate whether to wait for the list to be fully populated before returning the response. When traversing very a large set of files, it maybe a good idea to process the files as they get added to the list. In this case you should set this parameter to false. If this parameter is set to false, a null entry will be added to the end of the list to indicate that the directory search is complete. Example:
    boolean wait = false;
    java.util.List files = directory.getChildren(true, null, wait);
    if (wait){
        for (int i=0; i<files.size(); i++){
            System.out.println(files.get(i));
        }
    }
    else{
        Object obj;
        while (true){
            synchronized (files) {
                while (files.isEmpty()) {
                  try {
                      files.wait();
                  }
                  catch (InterruptedException e) {
                      break;
                  }
                }
                obj = files.remove(0);
                files.notifyAll();
            }

            if (obj==null){
                break;
            }
            else{
                System.out.println(obj);
                if (obj instanceof javaxt.io.File){
                    javaxt.io.File file = (javaxt.io.File) obj;
                }
                else if (obj instanceof javaxt.io.Directory){
                    javaxt.io.Directory dir = (javaxt.io.Directory) obj;
                }
            }
        }
    }
    
toString( ) returns String
Returns the full path to this directory, including the directory name.
hashCode( ) returns int
compareTo( Object obj ) returns int
equals( Object obj ) returns boolean
clone( ) returns Directory
Creates a copy of this object.
getEvents( ) returns List
Used to start monitoring changes made to the directory. Changes include creating, modifying or deleting files/folders found in this directory. Returns a list of Directory.Event(s). Clients can wait for new events using the wait() method. Recommend removing events from the list whenever !events.isEmpty(). Example:
    java.util.List events = directory.getEvents();
    while (true){

        Object obj;
        synchronized (events) {
            while (events.isEmpty()) {
              try {
                  events.wait();
              }
              catch (InterruptedException e) {
              }
            }

            obj = events.remove(0);
        }
        if (obj!=null){

            javaxt.io.Directory.Event event = (javaxt.io.Directory.Event) obj;
            System.out.println(event.toString());

          //Compare files before/after the event
            if (event.getEventID()==event.RENAME){
                System.out.println(
                    event.getOriginalFile().getName() + " vs " +
                    event.getFile().getName()
                );
            }
        }

     }
stop( ) returns void
Used to stop any worker threads that may be running (recursive search or event monitor).

Static Methods

getRootDirectories( ) returns Directory[]
Returns an array of root directories on the filesystem (e.g. "/" or C:\"). On Windows, this method will return all mounted drives, including any that might have been disconnected. The array will be empty if there are no root directories or if the set of roots could not be determined.

Public Classes