com.evanmclean.evlib.io
Class Folders

java.lang.Object
  extended by com.evanmclean.evlib.io.Folders

public final class Folders
extends Object

Perform certain folder/directory related operations. Also see Files.

Author:
Evan McLean McLean Computer Services (see the overview for copyright and licensing.)

Method Summary
static boolean clean(File path, boolean including_empty_files)
          Recursively clears a folder of any empty folders and optionally, zero-length files as well.
static void clear(File folder)
          Clear the contents of a folder.
static void copy(File from, File to)
          Will recursively copy a folder to the target path.
static File createTempFolder(String prefix, String suffix)
          Creates an new folder in the default temporary-file folder, using the given prefix and suffix to generate its name.
static File createTempFolder(String prefix, String suffix, File parent)
           Creates a new folder in the specified parent folder, using the given prefix and suffix strings to generate its name.
static void del(File path)
          Delete the folder (if it exists) and all its contents.
static boolean del(File path, boolean quietly)
          Delete the folder (if it exists) and all its contents.
static boolean delQuietly(File path)
          Delete the folder (if it exists) and all its contents.
static void forceMksClear(File folder)
          If the folder already exists, clear its contents, otherwise create it and all necessary parent folders - if the specified path is a file, delete it first then create the folder with the same name.
static boolean isAllEmpty(File path)
          Checks if the path has any non-zero length files, or is just folders and empty files (or doesn't even exist).
static boolean isEmpty(File path)
          Checks the path specified is a zero length file, an empty folder or does not exist.
static boolean isNotAllEmpty(File path)
          Checks if the path has any non-zero length files, or is just folders and empty files (or doesn't even exist).
static boolean isNotEmpty(File path)
          Checks the path specified is a zero length file, an empty folder or does not exist.
static boolean isNotOnlyFolders(File path)
          Checks if the path contains any files at all, or just zero or more folders and sub-folders.
static boolean isOnlyFolders(File path)
          Checks if the path contains any files at all, or just zero or more folders and sub-folders.
static void mk(File folder)
          Create the folder if it does not already exist.
static void mks(File folder)
          Create the folder and all necessary parent folders if they do not already exist.
static void mksClear(File folder)
          If the folder already exists, clear its contents, otherwise create it and all necessary parent folders.
static void move(File from, File to)
          Will try and rename a folder, and failing that will copy then delete.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

clean

public static boolean clean(File path,
                            boolean including_empty_files)
                     throws IOException
Recursively clears a folder of any empty folders and optionally, zero-length files as well.

Parameters:
path - The path to clear.
including_empty_files - Remove zero-length files as well as empty folders.
Returns:
True if the specified path ended up being completely removed.
Throws:
IOException

clear

public static void clear(File folder)
                  throws IOException
Clear the contents of a folder.

Parameters:
folder - The folder to delete.
Throws:
IOException - If unable to fully delete the contents of the folder or the folder doesn't exist.

copy

public static void copy(File from,
                        File to)
                 throws IOException
Will recursively copy a folder to the target path.

Parameters:
from - Where to copy from.
to - Where to copy to. This folder should either not exist or be an empty folder.
Throws:
IOException - If there was an error.

createTempFolder

public static File createTempFolder(String prefix,
                                    String suffix)
                             throws IOException
Creates an new folder in the default temporary-file folder, using the given prefix and suffix to generate its name. Invoking this method is equivalent to invoking createTempFolder(prefix, suffix, null).

Parameters:
prefix - The prefix string to be used in generating the folder's name; must be at least three characters long.
suffix - The suffix string to be used in generating the folder's name; may be null, in which case the suffix ".tmp" will be used.
Returns:
An abstract pathname denoting a newly-created empty folder.
Throws:
IllegalArgumentException - If the prefix argument contains fewer than three characters.
IOException - If a folder could not be created.

createTempFolder

public static File createTempFolder(String prefix,
                                    String suffix,
                                    File parent)
                             throws IOException

Creates a new folder in the specified parent folder, using the given prefix and suffix strings to generate its name. If this method returns successfully then it is guaranteed that:

  1. The folder denoted by the returned abstract pathname did not exist before this method was invoked, and
  2. Neither this method nor any of its variants will return the same abstract pathname again in the current invocation of the virtual machine.

Note: Folders are not automatically deleted on exit or anything. It is up to the application to cleanup as necessary.

The prefix argument must be at least three characters long. It is recommended that the prefix be a short, meaningful string such as "hjb" or "mail". The suffix argument may be null, in which case the suffix ".tmp" will be used.

The name of the new folder will be generated by concatenating the prefix, some internally-generated characters, and the suffix.

If the parent argument is null then the system-dependent default temporary-file folder will be used. The default temporary-file folder is specified by the system property java.io.tmpdir. On UNIX systems the default value of this property is typically "/tmp" or "/var/tmp"; on Microsoft Windows systems it is typically "C:\\WINNT\\TEMP". A different value may be given to this system property when the Java virtual machine is invoked, but programmatic changes to this property are not guaranteed to have any effect upon the temporary folder used by this method.

Parameters:
prefix - The prefix string to be used in generating the folder's name; must be at least three characters long.
suffix - The suffix string to be used in generating the folder's name; may be null, in which case the suffix ".tmp" will be used.
parent - The parent folder in which the folder is to be created, or null if the default temporary-file folder is to be used.
Returns:
An abstract pathname denoting a newly-created empty folder.
Throws:
IOException - If a file could not be created.
IllegalArgumentException - If the prefix argument contains fewer than three characters

del

public static void del(File path)
                throws IOException
Delete the folder (if it exists) and all its contents. If you only want to delete the folder if it is empty, use Files.del(File). Note, if given a file, it will just delete the file, equivalent behaviour to Files.del(File) in this case.

Parameters:
path - The folder to delete.
Throws:
IOException - If unable to fully delete the folder.

del

public static boolean del(File path,
                          boolean quietly)
                   throws IOException
Delete the folder (if it exists) and all its contents. If you only want to delete the folder if it is empty, use Files.del(File). Note, if given a file, it will just delete the file, equivalent behaviour to Files.del(File) in this case.

Parameters:
path - The folder to delete.
quietly - Only throw the IO exception if quietly is false.
Returns:
True if successfully deleted, false if failed to delete and quietly is true.
Throws:
IOException - If unable to fully delete the folder and quietly is false.

delQuietly

public static boolean delQuietly(File path)
Delete the folder (if it exists) and all its contents. Does not throw an exception on failure.

Parameters:
path - The folder to delete.
Returns:
True if successfully deleted, false otherwise.

forceMksClear

public static void forceMksClear(File folder)
                          throws IOException
If the folder already exists, clear its contents, otherwise create it and all necessary parent folders - if the specified path is a file, delete it first then create the folder with the same name.

Parameters:
folder - Folder to create or clear.
Throws:
IOException - If it could create or delete whatever it needed to.

isAllEmpty

public static boolean isAllEmpty(File path)
Checks if the path has any non-zero length files, or is just folders and empty files (or doesn't even exist).

Parameters:
path -
Returns:
False if the path exists and contains any non-zero length files.

isEmpty

public static boolean isEmpty(File path)
                       throws IOException
Checks the path specified is a zero length file, an empty folder or does not exist.

Parameters:
path - The path the check.
Returns:
True if a zero length file, an empty folder or does not exist.
Throws:
IOException - If folder does not represent a file or a folder.

isNotAllEmpty

public static boolean isNotAllEmpty(File path)
Checks if the path has any non-zero length files, or is just folders and empty files (or doesn't even exist).

Parameters:
path -
Returns:
True if the path exists and contains any non-zero length files.

isNotEmpty

public static boolean isNotEmpty(File path)
                          throws IOException
Checks the path specified is a zero length file, an empty folder or does not exist.

Parameters:
path - The path the check.
Returns:
False if a zero length file, an empty folder or does not exist.
Throws:
IOException - If folder does not represent a file or a folder.

isNotOnlyFolders

public static boolean isNotOnlyFolders(File path)
Checks if the path contains any files at all, or just zero or more folders and sub-folders.

Parameters:
path -
Returns:
True if the path exists and contains any files.

isOnlyFolders

public static boolean isOnlyFolders(File path)
Checks if the path contains any files at all, or just zero or more folders and sub-folders.

Parameters:
path -
Returns:
False if the path exists and contains any files.

mk

public static void mk(File folder)
               throws IOException
Create the folder if it does not already exist. This is basically the same as File.mkdir() but throws an exception on error instead of returning false.

Parameters:
folder - Folder to create.
Throws:
IOException - If it could not create the folder, or the path already exists and is not a folder.

mks

public static void mks(File folder)
                throws IOException
Create the folder and all necessary parent folders if they do not already exist. This is basically the same as File.mkdirs() but throws an exception on error instead of returning false.

Parameters:
folder - Folder to create.
Throws:
IOException - If it could not create the folder, or the path already exists and is not a folder.

mksClear

public static void mksClear(File folder)
                     throws IOException
If the folder already exists, clear its contents, otherwise create it and all necessary parent folders.

Parameters:
folder - Folder to create or clear.
Throws:
IOException - If it could create or delete whatever it needed to, or the path already exists and is not a folder.

move

public static void move(File from,
                        File to)
                 throws IOException
Will try and rename a folder, and failing that will copy then delete.

Parameters:
from - Folder to copy.
to - Where to copy to. This path must not already exist.
Throws:
IOException