org.apache.commons.io
Class FilenameUtils

java.lang.Object
  extended byorg.apache.commons.io.FilenameUtils

public class FilenameUtils
extends java.lang.Object

Common File manipulation routines through use of a filename.

Origin of code

Version:
$Id: FilenameUtils.java,v 1.5 2003/12/30 15:23:23 jeremias Exp $
Author:
Kevin A. Burton, Scott Sanders, Daniel Rall, Christoph.Reck, Peter Donald, Jeff Turner, Matthew Hawthorne, Jeremias Maerki

Constructor Summary
FilenameUtils()
           
 
Method Summary
static java.lang.String basename(java.lang.String filename)
          Deprecated. This method will be deleted before a 1.0 release TODO DELETE before 1.0
static java.lang.String basename(java.lang.String filename, java.lang.String suffix)
          Deprecated. This method will be deleted.
static java.lang.String catPath(java.lang.String lookupPath, java.lang.String path)
          Will concatenate 2 paths.
static void cleanDirectory(java.lang.String directory)
          Deprecated. Use FileUtils.cleanDirectory(File)
static void copyFileToDirectory(java.lang.String source, java.lang.String destinationDirectory)
          Deprecated. Use FileUtils.copyFileToDirectory(File, File)
static void deleteDirectory(java.lang.String directory)
          Deprecated. Use FileUtils.deleteDirectory(File)
static java.lang.String dirname(java.lang.String filename)
          Deprecated. Use FileUtils#getPath(File) TODO DELETE before 1.0
static java.lang.String extension(java.lang.String filename)
          Deprecated. Use FileUtils#getExtension(File) TODO probably duplicate method. See getExtension
static void fileDelete(java.lang.String fileName)
          Deletes a file.
static boolean fileExists(java.lang.String fileName)
          Check if a file exits.
static java.lang.String filename(java.lang.String filename)
          Deprecated. Use FileUtils#removeExtension(File) TODO DELETE before 1.0
static void forceDelete(java.lang.String file)
          Deprecated. Use FileUtils.forceDelete(File)
static java.lang.String getExtension(java.lang.String filename)
          Get extension from filename.
static java.io.File getFile(java.lang.String fileName)
          Deprecated. Use java.io.File#Constructor(String)
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 void mkdir(java.lang.String dir)
          Simple way to make a directory.
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 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.lang.String directory)
          Deprecated. Use FileUtils.sizeOfDirectory(File)
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FilenameUtils

public FilenameUtils()
Method Detail

fileExists

public static boolean fileExists(java.lang.String fileName)
Check if a file exits.

Parameters:
fileName - The name of the file to check.
Returns:
true if file exists.

fileDelete

public static void fileDelete(java.lang.String fileName)
Deletes a file.

Parameters:
fileName - The name of the file to delete.

mkdir

public static void mkdir(java.lang.String dir)
Simple way to make a directory. It also creates the parent directories if necessary.

Parameters:
dir - directory to create

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
fileSeparatorChar - the file separator character to use
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
fileSeparatorChar - the file separator character to use
Returns:
the filename minus path

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.

basename

public static java.lang.String basename(java.lang.String filename)
Deprecated. This method will be deleted before a 1.0 release TODO DELETE before 1.0

Returns the filename portion of a file specification string. Matches the equally named unix command.

Parameters:
filename - filename to inspect
Returns:
The filename string without extension.

basename

public static java.lang.String basename(java.lang.String filename,
                                        java.lang.String suffix)
Deprecated. This method will be deleted.

Returns the filename portion of a file specification string. Matches the equally named unix command.

Parameters:
filename - filename to inspect
suffix - additional remaining portion of name that if matches will be removed
Returns:
The filename string without the suffix.

forceDelete

public static void forceDelete(java.lang.String file)
                        throws java.io.IOException
Deprecated. Use FileUtils.forceDelete(File)

Delete a file. If file is directory delete it and all sub-directories.

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

cleanDirectory

public static void cleanDirectory(java.lang.String directory)
                           throws java.io.IOException
Deprecated. Use FileUtils.cleanDirectory(File)

Clean a directory without deleting it.

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

sizeOfDirectory

public static long sizeOfDirectory(java.lang.String directory)
Deprecated. Use FileUtils.sizeOfDirectory(File)

Recursively count size of a directory (sum of the length of all files).

Parameters:
directory - directory to inspect
Returns:
size of directory in bytes.

copyFileToDirectory

public static void copyFileToDirectory(java.lang.String source,
                                       java.lang.String destinationDirectory)
                                throws java.io.IOException,
                                       java.io.FileNotFoundException
Deprecated. Use FileUtils.copyFileToDirectory(File, File)

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.

deleteDirectory

public static void deleteDirectory(java.lang.String directory)
                            throws java.io.IOException
Deprecated. Use FileUtils.deleteDirectory(File)

Recursively delete a directory.

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

dirname

public static java.lang.String dirname(java.lang.String filename)
Deprecated. Use FileUtils#getPath(File) TODO DELETE before 1.0

Returns the directory path portion of a file specification string. Matches the equally named unix command.

Parameters:
filename - filename to inspect
Returns:
The directory portion excluding the ending file separator.

filename

public static java.lang.String filename(java.lang.String filename)
Deprecated. Use FileUtils#removeExtension(File) TODO DELETE before 1.0

Returns the filename portion of a file specification string.

Parameters:
filename - filename to inspect
Returns:
The filename string with extension.

extension

public static java.lang.String extension(java.lang.String filename)
Deprecated. Use FileUtils#getExtension(File) TODO probably duplicate method. See getExtension

Returns the extension portion of a file specification string. This everything after the last dot '.' in the filename (NOT including the dot).

Parameters:
filename - filename to inspect
Returns:
the extension

getFile

public static java.io.File getFile(java.lang.String fileName)
Deprecated. Use java.io.File#Constructor(String)

Creates a file handle.

Parameters:
fileName - The name of the file.
Returns:
A File instance.