org.apache.tuscany.sca.contribution.service.util
Class FileHelper

java.lang.Object
  extended by org.apache.tuscany.sca.contribution.service.util.FileHelper

public class FileHelper
extends java.lang.Object


Nested Class Summary
static class FileHelper.RegExpFilter
          A regular-expression based resource filter
 
Constructor Summary
FileHelper()
           
 
Method Summary
static void cleanDirectory(java.io.File directory)
          Clean a directory without deleting it.
static void copyDirectory(java.io.File srcDir, java.io.File destDir)
          Copies a whole directory to a new location preserving the file dates.
static void copyDirectory(java.io.File srcDir, java.io.File destDir, boolean preserveFileDate)
          Copies a whole directory to a new location.
static void copyDirectoryToDirectory(java.io.File srcDir, java.io.File destDir)
          Copies a directory to within another directory preserving the file dates.
static void copyFile(java.io.File srcFile, java.io.File destFile)
          Copies a file to a new location preserving the file date.
static void copyFile(java.io.File srcFile, java.io.File destFile, boolean preserveFileDate)
          Copies a file to a new location.
static void copyFileToDirectory(java.io.File srcFile, java.io.File destDir)
          Copies a file to a directory preserving the file date.
static void copyFileToDirectory(java.io.File srcFile, java.io.File destDir, boolean preserveFileDate)
          Copies a file to a directory optionally preserving the file date.
static void deleteDirectory(java.io.File directory)
          Recursively delete a directory.
static void forceDelete(java.io.File file)
          Delete a file.
static void forceDeleteOnExit(java.io.File file)
          Schedule a file to be deleted when JVM exits.
static void forceMkdir(java.io.File directory)
          Make a directory, including any necessary but nonexistent parent directories.
static java.lang.String getExtension(java.lang.String filename)
          Gets the extension of a filename.
static java.io.FileFilter getFileFilter(java.lang.String regExp, boolean ignoreCase)
           
static java.lang.String getName(java.lang.String fileName)
          Gets the name minus the path from a full filename.
static int indexOfExtension(java.lang.String filename)
          Returns the index of the last extension separator character, which is a dot.
static int indexOfLastSeparator(java.lang.String filename)
          Returns the index of the last directory separator character.
static java.io.File toFile(java.net.URL url)
          Convert from a URL to a File.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FileHelper

public FileHelper()
Method Detail

indexOfLastSeparator

public static int indexOfLastSeparator(java.lang.String filename)
Returns the index of the last directory separator character.

This method will handle a file in either Unix or Windows format. The position of the last forward or backslash is returned.

The output will be the same irrespective of the machine that the code is running on.

Parameters:
filename - the filename to find the last path separator in, null returns -1
Returns:
the index of the last separator character, or -1 if there is no such character

indexOfExtension

public static int indexOfExtension(java.lang.String filename)
Returns the index of the last extension separator character, which is a dot.

This method also checks that there is no directory separator after the last dot. To do this it uses indexOfLastSeparator(String) which will handle a file in either Unix or Windows format.

The output will be the same irrespective of the machine that the code is running on.

Parameters:
filename - the filename to find the last path separator in, null returns -1
Returns:
the index of the last separator character, or -1 if there is no such character

getName

public static java.lang.String getName(java.lang.String fileName)
Gets the name minus the path from a full filename.

This method will handle a file in either Unix or Windows format. The text after the last forward or backslash is returned.

 a/b/c.txt --> c.txt
 a.txt     --> a.txt
 a/b/c     --> c
 a/b/c/    --> ""
 

The output will be the same irrespective of the machine that the code is running on.

Parameters:
fileName - the filename to query, null returns null
Returns:
the name of the file without the path, or an empty string if none exists

getExtension

public static java.lang.String getExtension(java.lang.String filename)
Gets the extension of a filename.

This method returns the textual part of the filename after the last dot. There must be no directory separator after the dot.

 foo.txt      --> "txt"
 a/b/c.jpg    --> "jpg"
 a/b.txt/c    --> ""
 a/b/c        --> ""
 

The output will be the same irrespective of the machine that the code is running on.

Parameters:
filename - the filename to retrieve the extension of.
Returns:
the extension of the file or an empty string if none exists.

forceMkdir

public static void forceMkdir(java.io.File directory)
                       throws java.io.IOException
Make a directory, including any necessary but nonexistent parent directories. If there already exists a file with specified name or the directory cannot be created then an exception is thrown.

Parameters:
directory - directory to create, not null
Throws:
java.lang.NullPointerException - if the directory is null
java.io.IOException - if the directory cannot be created

forceDelete

public static void forceDelete(java.io.File file)
                        throws java.io.IOException
Delete a file. If file is a directory, delete it and all sub-directories.

The difference between File.delete() and this method are:

Parameters:
file - file or directory to delete, not null
Throws:
java.lang.NullPointerException - if the directory is null
java.io.IOException - in case deletion is unsuccessful

toFile

public static java.io.File toFile(java.net.URL url)
Convert from a URL to a File.

From version 1.1 this method will decode the URL. Syntax such as file:///my%20docs/file.txt will be correctly decoded to /my docs/file.txt.

Parameters:
url - the file URL to convert, null returns null
Returns:
the equivalent File object, or null if the URL's protocol is not file
Throws:
java.lang.IllegalArgumentException - if the file is incorrectly encoded

getFileFilter

public static java.io.FileFilter getFileFilter(java.lang.String regExp,
                                               boolean ignoreCase)

cleanDirectory

public static void cleanDirectory(java.io.File directory)
                           throws java.io.IOException
Clean a directory without deleting it.

Parameters:
directory - directory to clean
Throws:
java.io.IOException - in case cleaning is unsuccessful

copyDirectory

public static void copyDirectory(java.io.File srcDir,
                                 java.io.File destDir)
                          throws java.io.IOException
Copies a whole directory to a new location preserving the file dates.

This method copies the specified directory and all its child directories and files to the specified destination. The destination is the new location and name of the directory.

The destination directory is created if it does not exist. If the destination directory did exist, then this method merges the source with the destination, with the source taking precedence.

Parameters:
srcDir - an existing directory to copy, must not be null
destDir - the new directory, must not be null
Throws:
java.lang.NullPointerException - if source or destination is null
java.io.IOException - if source or destination is invalid
java.io.IOException - if an IO error occurs during copying
Since:
Commons IO 1.1

copyDirectory

public static void copyDirectory(java.io.File srcDir,
                                 java.io.File destDir,
                                 boolean preserveFileDate)
                          throws java.io.IOException
Copies a whole directory to a new location.

This method copies the contents of the specified source directory to within the specified destination directory.

The destination directory is created if it does not exist. If the destination directory did exist, then this method merges the source with the destination, with the source taking precedence.

Parameters:
srcDir - an existing directory to copy, must not be null
destDir - the new directory, must not be null
preserveFileDate - true if the file date of the copy should be the same as the original
Throws:
java.lang.NullPointerException - if source or destination is null
java.io.IOException - if source or destination is invalid
java.io.IOException - if an IO error occurs during copying
Since:
Commons IO 1.1

copyDirectoryToDirectory

public static void copyDirectoryToDirectory(java.io.File srcDir,
                                            java.io.File destDir)
                                     throws java.io.IOException
Copies a directory to within another directory preserving the file dates.

This method copies the source directory and all its contents to a directory of the same name in the specified destination directory.

The destination directory is created if it does not exist. If the destination directory did exist, then this method merges the source with the destination, with the source taking precedence.

Parameters:
srcDir - an existing directory to copy, must not be null
destDir - the directory to place the copy in, must not be null
Throws:
java.lang.NullPointerException - if source or destination is null
java.io.IOException - if source or destination is invalid
java.io.IOException - if an IO error occurs during copying
Since:
Commons IO 1.2

copyFile

public static void copyFile(java.io.File srcFile,
                            java.io.File destFile)
                     throws java.io.IOException
Copies a file to a new location preserving the file date.

This method copies the contents of the specified source file to the specified destination file. The directory holding the destination file is created if it does not exist. If the destination file exists, then this method will overwrite it.

Parameters:
srcFile - an existing file to copy, must not be null
destFile - the new file, must not be null
Throws:
java.lang.NullPointerException - if source or destination is null
java.io.IOException - if source or destination is invalid
java.io.IOException - if an IO error occurs during copying
See Also:
copyFileToDirectory(File, File)

copyFile

public static void copyFile(java.io.File srcFile,
                            java.io.File destFile,
                            boolean preserveFileDate)
                     throws java.io.IOException
Copies a file to a new location.

This method copies the contents of the specified source file to the specified destination file. The directory holding the destination file is created if it does not exist. If the destination file exists, then this method will overwrite it.

Parameters:
srcFile - an existing file to copy, must not be null
destFile - the new file, must not be null
preserveFileDate - true if the file date of the copy should be the same as the original
Throws:
java.lang.NullPointerException - if source or destination is null
java.io.IOException - if source or destination is invalid
java.io.IOException - if an IO error occurs during copying
See Also:
copyFileToDirectory(File, File, boolean)

copyFileToDirectory

public static void copyFileToDirectory(java.io.File srcFile,
                                       java.io.File destDir)
                                throws java.io.IOException
Copies a file to a directory preserving the file date.

This method copies the contents of the specified source file to a file of the same name in the specified destination directory. The destination directory is created if it does not exist. If the destination file exists, then this method will overwrite it.

Parameters:
srcFile - an existing file to copy, must not be null
destDir - the directory to place the copy in, must not be null
Throws:
java.lang.NullPointerException - if source or destination is null
java.io.IOException - if source or destination is invalid
java.io.IOException - if an IO error occurs during copying
See Also:
copyFile(File, File, boolean)

copyFileToDirectory

public static void copyFileToDirectory(java.io.File srcFile,
                                       java.io.File destDir,
                                       boolean preserveFileDate)
                                throws java.io.IOException
Copies a file to a directory optionally preserving the file date.

This method copies the contents of the specified source file to a file of the same name in the specified destination directory. The destination directory is created if it does not exist. If the destination file exists, then this method will overwrite it.

Parameters:
srcFile - an existing file to copy, must not be null
destDir - the directory to place the copy in, must not be null
preserveFileDate - true if the file date of the copy should be the same as the original
Throws:
java.lang.NullPointerException - if source or destination is null
java.io.IOException - if source or destination is invalid
java.io.IOException - if an IO error occurs during copying
Since:
Commons IO 1.3
See Also:
copyFile(File, File, boolean)

deleteDirectory

public static void deleteDirectory(java.io.File directory)
                            throws java.io.IOException
Recursively delete a directory.

Parameters:
directory - directory to delete
Throws:
java.io.IOException - in case deletion is unsuccessful

forceDeleteOnExit

public static void forceDeleteOnExit(java.io.File file)
                              throws java.io.IOException
Schedule a file to be deleted when JVM exits. If file is directory delete it and all sub-directories.

Parameters:
file - file or directory to delete, must not be null
Throws:
java.lang.NullPointerException - if the file is null
java.io.IOException - in case deletion is unsuccessful