com.senn.magic.io
Class Directory

java.lang.Object
  extended by com.senn.magic.io.Directory

public class Directory
extends Object

This class represents a directory on a file system. This class was created to make some typical directory-actions easier, since Java enforces users to work with File objects, which can be less straight forward than one would hope!

Since:
3.2
Author:
Bart Thierens
Last modification: 06/06/2010

Field Summary
protected  List<Directory> childDirectories
           
protected  List<File> childFiles
           
protected  File directoryFile
           
 
Constructor Summary
protected Directory(File file)
          Protected constructor, for internal use only.
  Directory(File file, boolean getSubChildren)
           
  Directory(String path, boolean getSubChildren)
           
 
Method Summary
 int childrenCount()
          Counts how many children are in the Directory.
 void copyTo(File file)
          Copies a file to the current Directory
 boolean createChildDirectories(String names)
          Create new nested child directories under the current Directory.
 boolean createChildDirectory(String name)
          Create a new child directory under the current Directory.
 boolean delete()
          Deletes the directory and all his children.
 int directoryCount()
          Counts how many actual folders are in the Directory.
 int fileCount()
          Counts how many actual files are in the Directory.
 List<Directory> getChildDirectories()
          Gets all the child-directories.
 List<File> getChildDirectoriesAsFile()
          Gets all the child-directories as File objects.
 List<File> getChildFiles()
          Gets all the child-files.
 File getDirectoryFile()
          Gets the File object that represents the directory
 String getName()
          Gets the name of the Directory.
protected  void processChildren(boolean getSubChildren)
          Protected method, for internal use only.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

childFiles

protected List<File> childFiles

childDirectories

protected List<Directory> childDirectories

directoryFile

protected File directoryFile
Constructor Detail

Directory

public Directory(String path,
                 boolean getSubChildren)
          throws DirectoryException
Parameters:
path - the path where the directory is located
getSubChildren - true if you want to include all children of children (of children of children of ...) in the result (tree-wise)
Throws:
DirectoryException

Directory

public Directory(File file,
                 boolean getSubChildren)
          throws DirectoryException
Parameters:
file - a File object that represents your directory
getSubChildren - true if you want to include all children of children (of children of children of ...) in the result (tree-wise)
Throws:
DirectoryException

Directory

protected Directory(File file)
             throws DirectoryException
Protected constructor, for internal use only.

Parameters:
file - a File object that represents a directory
Throws:
DirectoryException
Method Detail

processChildren

protected void processChildren(boolean getSubChildren)
                        throws DirectoryException
Protected method, for internal use only.
This method processes children.

Parameters:
getSubChildren - include children or not
Throws:
DirectoryException

getDirectoryFile

public File getDirectoryFile()
Gets the File object that represents the directory

Returns:
File - the directory as a File object

getChildFiles

public List<File> getChildFiles()
Gets all the child-files.
This means, all the File objects for which file.isFile() equals true.

Returns:
List - the list of child-files as File objects

getChildDirectories

public List<Directory> getChildDirectories()
Gets all the child-directories.
This means, all the Directory objects for which their respective File objects have true returned for file.isDirectory().

NOTE:
If you created a Directory object with @param getSubChildren = true, all of these Directory objects will contain a List with their children (who then again have a List for their children, and so on...).
This might affect performance, so watch out!

Returns:
List - the list of child-directories as Directory objects

getChildDirectoriesAsFile

public List<File> getChildDirectoriesAsFile()
Gets all the child-directories as File objects.

Returns:
List - the list of child-directories as File objects

createChildDirectory

public boolean createChildDirectory(String name)
                             throws DirectoryException
Create a new child directory under the current Directory.

Similar approach as new File().mkdir()

Parameters:
name - the name of the new directory
Returns:
boolean - true if it succeeded, false if it didn't
Throws:
DirectoryException
See Also:
Sun javadoc File.mkdir()

createChildDirectories

public boolean createChildDirectories(String names)
                               throws DirectoryException
Create new nested child directories under the current Directory.

Similar approach as: new File().mkdirs()

Parameters:
names - the name of the new directory
Returns:
boolean - true if it succeeded, false if it didn't
Throws:
DirectoryException
See Also:
Sun javadoc File.mkdirs()

copyTo

public void copyTo(File file)
            throws DirectoryException
Copies a file to the current Directory

Parameters:
file - the original file that you want to copy to the directory
Throws:
DirectoryException

childrenCount

public int childrenCount()
Counts how many children are in the Directory.

Returns:
int - the children count
See Also:
FileUtils.childrenCount(File)

fileCount

public int fileCount()
Counts how many actual files are in the Directory.

Returns:
int - the file count
See Also:
FileUtils.fileCount(File)

directoryCount

public int directoryCount()
Counts how many actual folders are in the Directory.

Returns:
int - the directory count
See Also:
FileUtils.directoryCount(File)

getName

public String getName()
Gets the name of the Directory.
Equivalent of directory.getDirectoryFile().getName()

Returns:
String - the name of the Directory
See Also:
getDirectoryFile()

delete

public boolean delete()
Deletes the directory and all his children.

Returns:
boolean - true if everything was deleted successfully, false if something went wrong
See Also:
FileUtils.deleteDir(File)