textbender.g.io
Class FileX

java.lang.Object
  extended by FileX

@ThreadSafe
public final class FileX
extends Object

File utilities.


Field Summary
static Pattern BODY_DOTX_PATTERN
          Pattern to split a filename (or path) into two groups: body and dot-extension.
 
Method Summary
static void copyAs(File target, File source)
           
static void copyAs(File target, File source, FileFilter fileFilter)
          Copies a file to a new file named as specified.
static void copyTo(File targetDirectory, File source)
           
static void copyTo(File targetDirectory, File source, FileFilter fileFilter)
           
static boolean createNewDirectory(File directory)
          Atomically creates a new, empty directory if and only if the directory does not yet exist.
static boolean deleteRecursive(File fileOrDirectory)
          As per File.delete() except it will work with non-empty directories.
static boolean deleteRecursiveFrom(File directory)
          Same as deleteRecursive(), except it works only on the contents of the specified directory.
static boolean renameFrom(File oldFile, File newFile)
          Tries hard to rename a file, despite bug 6213298.
static boolean renameFromDefaultsToCopy(File oldFile, File newFile)
          First tries renameFrom, and failing that, makes a copy.
static File tempDirectory()
          Returns a new instance of file system's temporary directory, as defined by System property 'java.io.tmpdir'.
static boolean testsCaseSensitive()
          Tests whether or not the file system is case sensitive, and returns true iff it is.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

BODY_DOTX_PATTERN

public static final Pattern BODY_DOTX_PATTERN
Pattern to split a filename (or path) into two groups: body and dot-extension. The body group (1) may include separators '/'. It will be neither empty nor null.

The extension group (2) will either include the preceding dot '.', or it will be empty. It will not be null.

Method Detail

copyAs

public static void copyAs(File target,
                          File source)
                   throws IOException
Parameters:
target - pathname to create as copy (will be overwritten if it already exists)
source - file or directory to copy (directory contents are recursively copied using copyTo)
Throws:
IOException

copyAs

public static void copyAs(File target,
                          File source,
                          FileFilter fileFilter)
                   throws IOException
Copies a file to a new file named as specified. No checking is done to prevent self-copying.

Parameters:
target - pathname to create as copy (will be overwritten if it already exists)
source - file or directory to copy (directory contents are recursively copied using copyTo)
fileFilter - to use, if the source is a directory (filter is applied to contents of directory, and recursively to contents of any sub-directories that themselves pass the filter)
Throws:
IOException

copyTo

public static void copyTo(File targetDirectory,
                          File source)
                   throws IOException
Parameters:
targetDirectory - in which to make copy (must already exist)
source - file or directory to copy (directories are recursively copied) overwriting any existing file or directory of the same name
Throws:
IOException

copyTo

public static void copyTo(File targetDirectory,
                          File source,
                          FileFilter fileFilter)
                   throws IOException
Parameters:
targetDirectory - in which to make copy (must already exist)
source - file or directory to copy (directories are recursively copied) overwriting any existing file or directory of the same name
fileFilter - to use, if the source is a directory (filter is applied to contents of directory, and recursively to contents of any sub-directories that themselves pass the filter)
Throws:
IOException

createNewDirectory

public static boolean createNewDirectory(File directory)
                                  throws IOException
Atomically creates a new, empty directory if and only if the directory does not yet exist. Similar to File.createNewFile(), but creates a directory. Also creates any necessary parent directories.

Parameters:
directory - to create if it does not already exist
Returns:
true if the directory was created; false if it already existed
Throws:
IOException - if the directory did not exist, and could not be created

deleteRecursive

public static boolean deleteRecursive(File fileOrDirectory)
As per File.delete() except it will work with non-empty directories.

Parameters:
fileOrDirectory - to delete
Returns:
true if entirely deleted; false otherwise
Throws:
SecurityException - if the application does not have permission to delete the fileOrDirectory

deleteRecursiveFrom

public static boolean deleteRecursiveFrom(File directory)
Same as deleteRecursive(), except it works only on the contents of the specified directory.

Parameters:
directory - whose contents to delete
Returns:
true if deleted; false otherwise
Throws:
SecurityException - if the application does not have permission to delete the fileOrDirectory

testsCaseSensitive

public static boolean testsCaseSensitive()
Tests whether or not the file system is case sensitive, and returns true iff it is.


renameFrom

public static boolean renameFrom(File oldFile,
                                 File newFile)
Tries hard to rename a file, despite bug 6213298.


renameFromDefaultsToCopy

public static boolean renameFromDefaultsToCopy(File oldFile,
                                               File newFile)
                                        throws IOException
First tries renameFrom, and failing that, makes a copy. Does not delete the old file.

Returns:
true if it had to resort to copying; false if the rename worked
Throws:
IOException

tempDirectory

public static File tempDirectory()
Returns a new instance of file system's temporary directory, as defined by System property 'java.io.tmpdir'.