JavaXT
|
|||||||||||||||||||||||||||||
Directory ClassUsed 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).
ConstructorsPropertiesPathSeparator
Public Methodsexists( ) 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. 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.
moveTo( Directory Destination, boolean Overwrite ) returns void Used to move a directory from one directory to another. 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. 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. 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.
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.
getFiles( boolean RecursiveSearch ) returns File[] Used to retrieve an array of files found in this directory.
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.
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.
getChildren( boolean RecursiveSearch, Object filter, boolean wait ) returns List Used to retrieve an list of files and folders found in this directory.
toString( ) returns String Returns the full path to this directory, including the directory name. equals( Object obj ) returns boolean Returns true if the given Object is a Directory (or java.io.File) that refers to the current directory. 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). Caller can wait for new events using the wait() method like this:
java.util.List events = directory.getEvents(); while (true){ Directory.Event event; synchronized (events) { synchronized (events) { while (events.isEmpty()) { try { events.wait(); } catch (InterruptedException e) {} } event = (Directory.Event) events.remove(0); } } if (event!=null){ System.out.println(event.toString()); if (event.getEventID()==event.RENAME){ System.out.println( event.getOriginalFile() + " vs " + event.getFile() ); } } }Note that in this example, we are removing events from the list in the order that they occur. It is critical to remove events from the list to as quickly as possible to avoid potential memory issues. Use the stop() method to stop monitoring events. Obviously you will need to start the monitor in a separate thread in order to call the stop() method. Example: //Start directory monitor new Thread(() -> { java.util.List events = directory.getEvents(); while (true){ ... } }).start(); //Stop directory monitor directory.stop(); stop( ) returns void Used to stop any worker threads that may be running (recursive search or event monitor). Static MethodsgetRootDirectories( ) 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 |