org.apache.avalon.excalibur.io
Class FileUtil

java.lang.Object
  extended byorg.apache.avalon.excalibur.io.FileUtil

public final class FileUtil
extends java.lang.Object

This class provides basic facilities for manipulating files and file paths.

Path-related methods

Methods exist to retrieve the components of a typical file path. For example /www/hosted/mysite/index.html, can be broken into:

There are also methods to concatenate two paths, resolve a path relative to a File and normalize(java.lang.String) a path.

File-related methods

There are methods to create a File from a URL, copy a File to a directory, copy a File to another File, copy a URL's contents to a File, as well as methods to delete and clean a directory.

Since:
4.0
Version:
CVS $Revision: 1.29 $ $Date: 2002/05/30 12:23:52 $
Author:
Peter Donald, Jeff Turner

Constructor Summary
private FileUtil()
          Private constructor to prevent instantiation.
 
Method Summary
static java.lang.String catPath(java.lang.String lookupPath, java.lang.String path)
          Will concatenate 2 paths.
static void cleanDirectory(java.io.File directory)
          Clean a directory without deleting it.
static void cleanDirectory(java.lang.String directory)
          Clean a directory without deleting it.
private static void cleanDirectoryOnExit(java.io.File directory)
          Clean a directory without deleting it.
static boolean contentEquals(java.io.File file1, java.io.File file2)
          Compare the contents of two files to determine if they are equal or not.
static void copyFile(java.io.File source, java.io.File destination)
          Copy file from source to destination.
static void copyFileToDirectory(java.io.File source, java.io.File destinationDirectory)
          Copy file from source to destination.
static void copyFileToDirectory(java.lang.String source, java.lang.String destinationDirectory)
          Copy file from source to destination.
static void copyURLToFile(java.net.URL source, java.io.File destination)
          Copies bytes from the URL source to a file destination.
static void deleteDirectory(java.io.File directory)
          Recursively delete a directory.
static void deleteDirectory(java.lang.String directory)
          Recursively delete a directory.
private static void deleteDirectoryOnExit(java.io.File directory)
          Recursively schedule directory for deletion on JVM exit.
static void forceDelete(java.io.File file)
          Delete a file.
static void forceDelete(java.lang.String 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 file)
          Make a directory.
static java.lang.String getExtension(java.lang.String filename)
          Get extension from filename.
static java.lang.String getPath(java.lang.String filepath)
          Get path from filename.
static java.lang.String getPath(java.lang.String filepath, char fileSeparatorChar)
          Get path from filename.
static java.lang.String normalize(java.lang.String path)
          Normalize a path.
static java.lang.String removeExtension(java.lang.String filename)
          Remove extension from filename.
static java.lang.String removeExtention(java.lang.String filename)
          Deprecated. Use removeExtension as removeExtention is mispelled
static java.lang.String removePath(java.lang.String filepath)
          Remove path from filename.
static java.lang.String removePath(java.lang.String filepath, char fileSeparatorChar)
          Remove path from filename.
static java.io.File resolveFile(java.io.File baseFile, java.lang.String filename)
          Resolve a file filename to it's canonical form.
static long sizeOfDirectory(java.io.File directory)
          Recursively count size of a directory.
static long sizeOfDirectory(java.lang.String directory)
          Recursively count size of a directory.
static java.io.File toFile(java.net.URL url)
          Convert from a URL to a File.
static java.net.URL[] toURLs(java.io.File[] files)
          Convert the array of Files into a list of URLs.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FileUtil

private FileUtil()
Private constructor to prevent instantiation.

Method Detail

contentEquals

public static boolean contentEquals(java.io.File file1,
                                    java.io.File file2)
                             throws java.io.IOException
Compare the contents of two files to determine if they are equal or not.

Parameters:
file1 - the first file
file2 - the second file
Returns:
true if the content of the files are equal or they both don't exist, false otherwise
Throws:
java.io.IOException

toFile

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

Parameters:
url - File URL.
Returns:
The equivalent File object, or null if the URL's protocol is not file

toURLs

public static java.net.URL[] toURLs(java.io.File[] files)
                             throws java.io.IOException
Convert the array of Files into a list of URLs.

Parameters:
files - the array of files
Returns:
the array of URLs
Throws:
java.io.IOException - if an error occurs

removeExtention

public static java.lang.String removeExtention(java.lang.String filename)
Deprecated. Use removeExtension as removeExtention is mispelled

Remove extension from filename. ie
 foo.txt    --> foo
 a\b\c.jpg --> a\b\c
 a\b\c     --> a\b\c
 

Parameters:
filename - the filename
Returns:
the filename minus extension

removeExtension

public static java.lang.String removeExtension(java.lang.String filename)
Remove extension from filename. ie
 foo.txt    --> foo
 a\b\c.jpg --> a\b\c
 a\b\c     --> a\b\c
 

Parameters:
filename - the filename
Returns:
the filename minus extension

getExtension

public static java.lang.String getExtension(java.lang.String filename)
Get extension from filename. ie
 foo.txt    --> "txt"
 a\b\c.jpg --> "jpg"
 a\b\c     --> ""
 

Parameters:
filename - the filename
Returns:
the extension of filename or "" if none

removePath

public static java.lang.String removePath(java.lang.String filepath)
Remove path from filename. Equivalent to the unix command basename ie.
 a/b/c.txt --> c.txt
 a.txt     --> a.txt
 

Parameters:
filepath - the filepath
Returns:
the filename minus path

removePath

public static java.lang.String removePath(java.lang.String filepath,
                                          char fileSeparatorChar)
Remove path from filename. ie.
 a/b/c.txt --> c.txt
 a.txt     --> a.txt
 

Parameters:
filepath - the filepath
Returns:
the filename minus path

getPath

public static java.lang.String getPath(java.lang.String filepath)
Get path from filename. Roughly equivalent to the unix command dirname. ie.
 a/b/c.txt --> a/b
 a.txt     --> ""
 

Parameters:
filepath - the filepath
Returns:
the filename minus path

getPath

public static java.lang.String getPath(java.lang.String filepath,
                                       char fileSeparatorChar)
Get path from filename. ie.
 a/b/c.txt --> a/b
 a.txt     --> ""
 

Parameters:
filepath - the filepath
Returns:
the filename minus path

copyFileToDirectory

public static void copyFileToDirectory(java.lang.String source,
                                       java.lang.String destinationDirectory)
                                throws java.io.IOException
Copy file from source to destination. If destinationDirectory does not exist, it (and any parent directories) will be created. If a file source in destinationDirectory exists, it will be overwritten.

Parameters:
source - An existing File to copy.
destinationDirectory - A directory to copy source into.
Throws:
java.io.FileNotFoundException - if source isn't a normal file.
java.lang.IllegalArgumentException - if destinationDirectory isn't a directory.
java.io.IOException - if source does not exist, the file in destinationDirectory cannot be written to, or an IO error occurs during copying.

copyFileToDirectory

public static void copyFileToDirectory(java.io.File source,
                                       java.io.File destinationDirectory)
                                throws java.io.IOException
Copy file from source to destination. If destinationDirectory does not exist, it (and any parent directories) will be created. If a file source in destinationDirectory exists, it will be overwritten.

Parameters:
source - An existing File to copy.
destinationDirectory - A directory to copy source into.
Throws:
java.io.FileNotFoundException - if source isn't a normal file.
java.lang.IllegalArgumentException - if destinationDirectory isn't a directory.
java.io.IOException - if source does not exist, the file in destinationDirectory cannot be written to, or an IO error occurs during copying.

copyFile

public static void copyFile(java.io.File source,
                            java.io.File destination)
                     throws java.io.IOException
Copy file from source to destination. The directories up to destination will be created if they don't already exist. destination will be overwritten if it already exists.

Parameters:
source - An existing non-directory File to copy bytes from.
destination - A non-directory File to write bytes to (possibly overwriting).
Throws:
java.io.IOException - if source does not exist, destination cannot be written to, or an IO error occurs during copying.
java.io.FileNotFoundException - if destination is a directory (use copyFileToDirectory(java.lang.String, java.lang.String)).

copyURLToFile

public static void copyURLToFile(java.net.URL source,
                                 java.io.File destination)
                          throws java.io.IOException
Copies bytes from the URL source to a file destination. The directories up to destination will be created if they don't already exist. destination will be overwritten if it already exists.

Parameters:
source - A URL to copy bytes from.
destination - A non-directory File to write bytes to (possibly overwriting).
Throws:
java.io.IOException - if
  • source URL cannot be opened
  • destination cannot be written to
  • an IO error occurs during copying

normalize

public static java.lang.String normalize(java.lang.String path)
Normalize a path. Eliminates "/../" and "/./" in a string. Returns null if the ..'s went past the root. Eg:
 /foo//               -->     /foo/
 /foo/./              -->     /foo/
 /foo/../bar          -->     /bar
 /foo/../bar/         -->     /bar/
 /foo/../bar/../baz   -->     /baz
 //foo//./bar         -->     /foo/bar
 /../                 -->     null
 

Parameters:
path - the path to normalize
Returns:
the normalized String, or null if too many ..'s.

catPath

public static java.lang.String catPath(java.lang.String lookupPath,
                                       java.lang.String path)
Will concatenate 2 paths. Paths with .. will be properly handled.

Eg.,
/a/b/c + d = /a/b/d
/a/b/c + ../d = /a/d

Thieved from Tomcat sources...

Returns:
The concatenated paths, or null if error occurs

resolveFile

public static java.io.File resolveFile(java.io.File baseFile,
                                       java.lang.String filename)
Resolve a file filename to it's canonical form. If filename is relative (doesn't start with /), it will be resolved relative to baseFile, otherwise it is treated as a normal root-relative path.

Parameters:
baseFile - Where to resolve filename from, if filename is relative.
filename - Absolute or relative file path to resolve.
Returns:
The canonical File of filename.

forceDelete

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

Throws:
java.io.IOException

forceDelete

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

Throws:
java.io.IOException

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.

Throws:
java.io.IOException

deleteDirectoryOnExit

private static void deleteDirectoryOnExit(java.io.File directory)
                                   throws java.io.IOException
Recursively schedule directory for deletion on JVM exit.

Throws:
java.io.IOException

cleanDirectoryOnExit

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

Throws:
java.io.IOException

forceMkdir

public static void forceMkdir(java.io.File file)
                       throws java.io.IOException
Make a directory. If there already exists a file with specified name or the directory is unable to be created then an exception is thrown.

Throws:
java.io.IOException

deleteDirectory

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

Throws:
java.io.IOException

deleteDirectory

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

Throws:
java.io.IOException

cleanDirectory

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

Throws:
java.io.IOException

cleanDirectory

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

Throws:
java.io.IOException

sizeOfDirectory

public static long sizeOfDirectory(java.lang.String directory)
Recursively count size of a directory.

Returns:
size of directory in bytes.

sizeOfDirectory

public static long sizeOfDirectory(java.io.File directory)
Recursively count size of a directory.

Returns:
size of directory in bytes.