FileDocCategorySizeDatePackage
FilenameUtils.javaAPI DocAndroid 1.5 API48951Wed May 06 22:42:46 BST 2009org.apache.commons.io

FilenameUtils

public class FilenameUtils extends Object
General filename and filepath manipulation utilities.

When dealing with filenames you can hit problems when moving from a Windows based development machine to a Unix based production machine. This class aims to help avoid those problems.

NOTE: You may be able to avoid using this class entirely simply by using JDK {@link java.io.File File} objects and the two argument constructor {@link java.io.File#File(java.io.File, java.lang.String) File(File,String)}.

Most methods on this class are designed to work the same on both Unix and Windows. Those that don't include 'System', 'Unix' or 'Windows' in their name.

Most methods recognise both separators (forward and back), and both sets of prefixes. See the javadoc of each method for details.

This class defines six components within a filename (example C:\dev\project\file.txt):

  • the prefix - C:\
  • the path - dev\project\
  • the full path - C:\dev\project\
  • the name - file.txt
  • the base name - file
  • the extension - txt
Note that this class works best if directory filenames end with a separator. If you omit the last separator, it is impossible to determine if the filename corresponds to a file or a directory. As a result, we have chosen to say it corresponds to a file.

This class only supports Unix and Windows style names. Prefixes are matched as follows:

Windows:
a\b\c.txt --> "" --> relative
\a\b\c.txt --> "\" --> current drive absolute
C:a\b\c.txt --> "C:" --> drive relative
C:\a\b\c.txt --> "C:\" --> absolute
\\server\a\b\c.txt --> "\\server\" --> UNC

Unix:
a/b/c.txt --> "" --> relative
/a/b/c.txt --> "/" --> absolute
~/a/b/c.txt --> "~/" --> current user
~ --> "~/" --> current user (slash added)
~user/a/b/c.txt --> "~user/" --> named user
~user --> "~user/" --> named user (slash added)
Both prefix styles are matched always, irrespective of the machine that you are currently running on.

Origin of code: Excalibur, Alexandria, Tomcat, Commons-Utils.

author
Kevin A. Burton
author
Scott Sanders
author
Daniel Rall
author
Christoph.Reck
author
Peter Donald
author
Jeff Turner
author
Matthew Hawthorne
author
Martin Cooper
author
Jeremias Maerki
author
Stephen Colebourne
version
$Id: FilenameUtils.java 609870 2008-01-08 04:46:26Z niallp $
since
Commons IO 1.1

Fields Summary
public static final char
EXTENSION_SEPARATOR
The extension separator character.
public static final String
EXTENSION_SEPARATOR_STR
The extension separator String.
private static final char
UNIX_SEPARATOR
The Unix separator character.
private static final char
WINDOWS_SEPARATOR
The Windows separator character.
private static final char
SYSTEM_SEPARATOR
The system separator character.
private static final char
OTHER_SEPARATOR
The separator character that is the opposite of the system separator.
Constructors Summary
public FilenameUtils()
Instances should NOT be constructed in standard programming.

     
        if (isSystemWindows()) {
            OTHER_SEPARATOR = UNIX_SEPARATOR;
        } else {
            OTHER_SEPARATOR = WINDOWS_SEPARATOR;
        }
    
        super();
    
Methods Summary
public static java.lang.Stringconcat(java.lang.String basePath, java.lang.String fullFilenameToAdd)
Concatenates a filename to a base path using normal command line style rules.

The effect is equivalent to resultant directory after changing directory to the first argument, followed by changing directory to the second argument.

The first argument is the base path, the second is the path to concatenate. The returned path is always normalized via {@link #normalize(String)}, thus .. is handled.

If pathToAdd is absolute (has an absolute prefix), then it will be normalized and returned. Otherwise, the paths will be joined, normalized and returned.

The output will be the same on both Unix and Windows except for the separator character.

/foo/ + bar --> /foo/bar
/foo + bar --> /foo/bar
/foo + /bar --> /bar
/foo + C:/bar --> C:/bar
/foo + C:bar --> C:bar (*)
/foo/a/ + ../bar --> foo/bar
/foo/ + ../../bar --> null
/foo/ + /bar --> /bar
/foo/.. + /bar --> /bar
/foo + bar/c.txt --> /foo/bar/c.txt
/foo/c.txt + bar --> /foo/c.txt/bar (!)
(*) Note that the Windows relative drive prefix is unreliable when used with this method. (!) Note that the first parameter must be a path. If it ends with a name, then the name will be built into the concatenated path. If this might be a problem, use {@link #getFullPath(String)} on the base path argument.

param
basePath the base path to attach to, always treated as a path
param
fullFilenameToAdd the filename (or path) to attach to the base
return
the concatenated path, or null if invalid

        int prefix = getPrefixLength(fullFilenameToAdd);
        if (prefix < 0) {
            return null;
        }
        if (prefix > 0) {
            return normalize(fullFilenameToAdd);
        }
        if (basePath == null) {
            return null;
        }
        int len = basePath.length();
        if (len == 0) {
            return normalize(fullFilenameToAdd);
        }
        char ch = basePath.charAt(len - 1);
        if (isSeparator(ch)) {
            return normalize(basePath + fullFilenameToAdd);
        } else {
            return normalize(basePath + '/" + fullFilenameToAdd);
        }
    
private static java.lang.StringdoGetFullPath(java.lang.String filename, boolean includeSeparator)
Does the work of getting the path.

param
filename the filename
param
includeSeparator true to include the end separator
return
the path

        if (filename == null) {
            return null;
        }
        int prefix = getPrefixLength(filename);
        if (prefix < 0) {
            return null;
        }
        if (prefix >= filename.length()) {
            if (includeSeparator) {
                return getPrefix(filename);  // add end slash if necessary
            } else {
                return filename;
            }
        }
        int index = indexOfLastSeparator(filename);
        if (index < 0) {
            return filename.substring(0, prefix);
        }
        int end = index + (includeSeparator ?  1 : 0);
        return filename.substring(0, end);
    
private static java.lang.StringdoGetPath(java.lang.String filename, int separatorAdd)
Does the work of getting the path.

param
filename the filename
param
separatorAdd 0 to omit the end separator, 1 to return it
return
the path

        if (filename == null) {
            return null;
        }
        int prefix = getPrefixLength(filename);
        if (prefix < 0) {
            return null;
        }
        int index = indexOfLastSeparator(filename);
        if (prefix >= filename.length() || index < 0) {
            return "";
        }
        return filename.substring(prefix, index + separatorAdd);
    
private static java.lang.StringdoNormalize(java.lang.String filename, boolean keepSeparator)
Internal method to perform the normalization.

param
filename the filename
param
keepSeparator true to keep the final separator
return
the normalized filename

        if (filename == null) {
            return null;
        }
        int size = filename.length();
        if (size == 0) {
            return filename;
        }
        int prefix = getPrefixLength(filename);
        if (prefix < 0) {
            return null;
        }
        
        char[] array = new char[size + 2];  // +1 for possible extra slash, +2 for arraycopy
        filename.getChars(0, filename.length(), array, 0);
        
        // fix separators throughout
        for (int i = 0; i < array.length; i++) {
            if (array[i] == OTHER_SEPARATOR) {
                array[i] = SYSTEM_SEPARATOR;
            }
        }
        
        // add extra separator on the end to simplify code below
        boolean lastIsDirectory = true;
        if (array[size - 1] != SYSTEM_SEPARATOR) {
            array[size++] = SYSTEM_SEPARATOR;
            lastIsDirectory = false;
        }
        
        // adjoining slashes
        for (int i = prefix + 1; i < size; i++) {
            if (array[i] == SYSTEM_SEPARATOR && array[i - 1] == SYSTEM_SEPARATOR) {
                System.arraycopy(array, i, array, i - 1, size - i);
                size--;
                i--;
            }
        }
        
        // dot slash
        for (int i = prefix + 1; i < size; i++) {
            if (array[i] == SYSTEM_SEPARATOR && array[i - 1] == '." &&
                    (i == prefix + 1 || array[i - 2] == SYSTEM_SEPARATOR)) {
                if (i == size - 1) {
                    lastIsDirectory = true;
                }
                System.arraycopy(array, i + 1, array, i - 1, size - i);
                size -=2;
                i--;
            }
        }
        
        // double dot slash
        outer:
        for (int i = prefix + 2; i < size; i++) {
            if (array[i] == SYSTEM_SEPARATOR && array[i - 1] == '." && array[i - 2] == '." &&
                    (i == prefix + 2 || array[i - 3] == SYSTEM_SEPARATOR)) {
                if (i == prefix + 2) {
                    return null;
                }
                if (i == size - 1) {
                    lastIsDirectory = true;
                }
                int j;
                for (j = i - 4 ; j >= prefix; j--) {
                    if (array[j] == SYSTEM_SEPARATOR) {
                        // remove b/../ from a/b/../c
                        System.arraycopy(array, i + 1, array, j + 1, size - i);
                        size -= (i - j);
                        i = j + 1;
                        continue outer;
                    }
                }
                // remove a/../ from a/../c
                System.arraycopy(array, i + 1, array, prefix, size - i);
                size -= (i + 1 - prefix);
                i = prefix + 1;
            }
        }
        
        if (size <= 0) {  // should never be less than 0
            return "";
        }
        if (size <= prefix) {  // should never be less than prefix
            return new String(array, 0, size);
        }
        if (lastIsDirectory && keepSeparator) {
            return new String(array, 0, size);  // keep trailing separator
        }
        return new String(array, 0, size - 1);  // lose trailing separator
    
public static booleanequals(java.lang.String filename1, java.lang.String filename2)
Checks whether two filenames are equal exactly.

No processing is performed on the filenames other than comparison, thus this is merely a null-safe case-sensitive equals.

param
filename1 the first filename to query, may be null
param
filename2 the second filename to query, may be null
return
true if the filenames are equal, null equals null
see
IOCase#SENSITIVE

        return equals(filename1, filename2, false, IOCase.SENSITIVE);
    
public static booleanequals(java.lang.String filename1, java.lang.String filename2, boolean normalized, org.apache.commons.io.IOCase caseSensitivity)
Checks whether two filenames are equal, optionally normalizing and providing control over the case-sensitivity.

param
filename1 the first filename to query, may be null
param
filename2 the second filename to query, may be null
param
normalized whether to normalize the filenames
param
caseSensitivity what case sensitivity rule to use, null means case-sensitive
return
true if the filenames are equal, null equals null
since
Commons IO 1.3

        
        if (filename1 == null || filename2 == null) {
            return filename1 == filename2;
        }
        if (normalized) {
            filename1 = normalize(filename1);
            filename2 = normalize(filename2);
            if (filename1 == null || filename2 == null) {
                throw new NullPointerException(
                    "Error normalizing one or both of the file names");
            }
        }
        if (caseSensitivity == null) {
            caseSensitivity = IOCase.SENSITIVE;
        }
        return caseSensitivity.checkEquals(filename1, filename2);
    
public static booleanequalsNormalized(java.lang.String filename1, java.lang.String filename2)
Checks whether two filenames are equal after both have been normalized.

Both filenames are first passed to {@link #normalize(String)}. The check is then performed in a case-sensitive manner.

param
filename1 the first filename to query, may be null
param
filename2 the second filename to query, may be null
return
true if the filenames are equal, null equals null
see
IOCase#SENSITIVE

        return equals(filename1, filename2, true, IOCase.SENSITIVE);
    
public static booleanequalsNormalizedOnSystem(java.lang.String filename1, java.lang.String filename2)
Checks whether two filenames are equal after both have been normalized and using the case rules of the system.

Both filenames are first passed to {@link #normalize(String)}. The check is then performed case-sensitive on Unix and case-insensitive on Windows.

param
filename1 the first filename to query, may be null
param
filename2 the second filename to query, may be null
return
true if the filenames are equal, null equals null
see
IOCase#SYSTEM

        return equals(filename1, filename2, true, IOCase.SYSTEM);
    
public static booleanequalsOnSystem(java.lang.String filename1, java.lang.String filename2)
Checks whether two filenames are equal using the case rules of the system.

No processing is performed on the filenames other than comparison. The check is case-sensitive on Unix and case-insensitive on Windows.

param
filename1 the first filename to query, may be null
param
filename2 the second filename to query, may be null
return
true if the filenames are equal, null equals null
see
IOCase#SYSTEM

        return equals(filename1, filename2, false, IOCase.SYSTEM);
    
public static java.lang.StringgetBaseName(java.lang.String filename)
Gets the base name, minus the full path and extension, from a full filename.

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

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

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

param
filename the filename to query, null returns null
return
the name of the file without the path, or an empty string if none exists

        return removeExtension(getName(filename));
    
public static java.lang.StringgetExtension(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.

param
filename the filename to retrieve the extension of.
return
the extension of the file or an empty string if none exists.

        if (filename == null) {
            return null;
        }
        int index = indexOfExtension(filename);
        if (index == -1) {
            return "";
        } else {
            return filename.substring(index + 1);
        }
    
public static java.lang.StringgetFullPath(java.lang.String filename)
Gets the full path from a full filename, which is the prefix + path.

This method will handle a file in either Unix or Windows format. The method is entirely text based, and returns the text before and including the last forward or backslash.

C:\a\b\c.txt --> C:\a\b\
~/a/b/c.txt --> ~/a/b/
a.txt --> ""
a/b/c --> a/b/
a/b/c/ --> a/b/c/
C: --> C:
C:\ --> C:\
~ --> ~/
~/ --> ~/
~user --> ~user/
~user/ --> ~user/

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

param
filename the filename to query, null returns null
return
the path of the file, an empty string if none exists, null if invalid

        return doGetFullPath(filename, true);
    
public static java.lang.StringgetFullPathNoEndSeparator(java.lang.String filename)
Gets the full path from a full filename, which is the prefix + path, and also excluding the final directory separator.

This method will handle a file in either Unix or Windows format. The method is entirely text based, and returns the text before the last forward or backslash.

C:\a\b\c.txt --> C:\a\b
~/a/b/c.txt --> ~/a/b
a.txt --> ""
a/b/c --> a/b
a/b/c/ --> a/b/c
C: --> C:
C:\ --> C:\
~ --> ~
~/ --> ~
~user --> ~user
~user/ --> ~user

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

param
filename the filename to query, null returns null
return
the path of the file, an empty string if none exists, null if invalid

        return doGetFullPath(filename, false);
    
public static java.lang.StringgetName(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.

param
filename the filename to query, null returns null
return
the name of the file without the path, or an empty string if none exists

        if (filename == null) {
            return null;
        }
        int index = indexOfLastSeparator(filename);
        return filename.substring(index + 1);
    
public static java.lang.StringgetPath(java.lang.String filename)
Gets the path from a full filename, which excludes the prefix.

This method will handle a file in either Unix or Windows format. The method is entirely text based, and returns the text before and including the last forward or backslash.

C:\a\b\c.txt --> a\b\
~/a/b/c.txt --> a/b/
a.txt --> ""
a/b/c --> a/b/
a/b/c/ --> a/b/c/

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

This method drops the prefix from the result. See {@link #getFullPath(String)} for the method that retains the prefix.

param
filename the filename to query, null returns null
return
the path of the file, an empty string if none exists, null if invalid

        return doGetPath(filename, 1);
    
public static java.lang.StringgetPathNoEndSeparator(java.lang.String filename)
Gets the path from a full filename, which excludes the prefix, and also excluding the final directory separator.

This method will handle a file in either Unix or Windows format. The method is entirely text based, and returns the text before the last forward or backslash.

C:\a\b\c.txt --> a\b
~/a/b/c.txt --> a/b
a.txt --> ""
a/b/c --> a/b
a/b/c/ --> a/b/c

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

This method drops the prefix from the result. See {@link #getFullPathNoEndSeparator(String)} for the method that retains the prefix.

param
filename the filename to query, null returns null
return
the path of the file, an empty string if none exists, null if invalid

        return doGetPath(filename, 0);
    
public static java.lang.StringgetPrefix(java.lang.String filename)
Gets the prefix from a full filename, such as C:/ or ~/.

This method will handle a file in either Unix or Windows format. The prefix includes the first slash in the full filename where applicable.

Windows:
a\b\c.txt --> "" --> relative
\a\b\c.txt --> "\" --> current drive absolute
C:a\b\c.txt --> "C:" --> drive relative
C:\a\b\c.txt --> "C:\" --> absolute
\\server\a\b\c.txt --> "\\server\" --> UNC

Unix:
a/b/c.txt --> "" --> relative
/a/b/c.txt --> "/" --> absolute
~/a/b/c.txt --> "~/" --> current user
~ --> "~/" --> current user (slash added)
~user/a/b/c.txt --> "~user/" --> named user
~user --> "~user/" --> named user (slash added)

The output will be the same irrespective of the machine that the code is running on. ie. both Unix and Windows prefixes are matched regardless.

param
filename the filename to query, null returns null
return
the prefix of the file, null if invalid

        if (filename == null) {
            return null;
        }
        int len = getPrefixLength(filename);
        if (len < 0) {
            return null;
        }
        if (len > filename.length()) {
            return filename + UNIX_SEPARATOR;  // we know this only happens for unix
        }
        return filename.substring(0, len);
    
public static intgetPrefixLength(java.lang.String filename)
Returns the length of the filename prefix, such as C:/ or ~/.

This method will handle a file in either Unix or Windows format.

The prefix length includes the first slash in the full filename if applicable. Thus, it is possible that the length returned is greater than the length of the input string.

Windows:
a\b\c.txt --> "" --> relative
\a\b\c.txt --> "\" --> current drive absolute
C:a\b\c.txt --> "C:" --> drive relative
C:\a\b\c.txt --> "C:\" --> absolute
\\server\a\b\c.txt --> "\\server\" --> UNC

Unix:
a/b/c.txt --> "" --> relative
/a/b/c.txt --> "/" --> absolute
~/a/b/c.txt --> "~/" --> current user
~ --> "~/" --> current user (slash added)
~user/a/b/c.txt --> "~user/" --> named user
~user --> "~user/" --> named user (slash added)

The output will be the same irrespective of the machine that the code is running on. ie. both Unix and Windows prefixes are matched regardless.

param
filename the filename to find the prefix in, null returns -1
return
the length of the prefix, -1 if invalid or null

        if (filename == null) {
            return -1;
        }
        int len = filename.length();
        if (len == 0) {
            return 0;
        }
        char ch0 = filename.charAt(0);
        if (ch0 == ':") {
            return -1;
        }
        if (len == 1) {
            if (ch0 == '~") {
                return 2;  // return a length greater than the input
            }
            return (isSeparator(ch0) ? 1 : 0);
        } else {
            if (ch0 == '~") {
                int posUnix = filename.indexOf(UNIX_SEPARATOR, 1);
                int posWin = filename.indexOf(WINDOWS_SEPARATOR, 1);
                if (posUnix == -1 && posWin == -1) {
                    return len + 1;  // return a length greater than the input
                }
                posUnix = (posUnix == -1 ? posWin : posUnix);
                posWin = (posWin == -1 ? posUnix : posWin);
                return Math.min(posUnix, posWin) + 1;
            }
            char ch1 = filename.charAt(1);
            if (ch1 == ':") {
                ch0 = Character.toUpperCase(ch0);
                if (ch0 >= 'A" && ch0 <= 'Z") {
                    if (len == 2 || isSeparator(filename.charAt(2)) == false) {
                        return 2;
                    }
                    return 3;
                }
                return -1;
                
            } else if (isSeparator(ch0) && isSeparator(ch1)) {
                int posUnix = filename.indexOf(UNIX_SEPARATOR, 2);
                int posWin = filename.indexOf(WINDOWS_SEPARATOR, 2);
                if ((posUnix == -1 && posWin == -1) || posUnix == 2 || posWin == 2) {
                    return -1;
                }
                posUnix = (posUnix == -1 ? posWin : posUnix);
                posWin = (posWin == -1 ? posUnix : posWin);
                return Math.min(posUnix, posWin) + 1;
            } else {
                return (isSeparator(ch0) ? 1 : 0);
            }
        }
    
public static intindexOfExtension(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 {@link #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.

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

        if (filename == null) {
            return -1;
        }
        int extensionPos = filename.lastIndexOf(EXTENSION_SEPARATOR);
        int lastSeparator = indexOfLastSeparator(filename);
        return (lastSeparator > extensionPos ? -1 : extensionPos);
    
public static intindexOfLastSeparator(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.

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

        if (filename == null) {
            return -1;
        }
        int lastUnixPos = filename.lastIndexOf(UNIX_SEPARATOR);
        int lastWindowsPos = filename.lastIndexOf(WINDOWS_SEPARATOR);
        return Math.max(lastUnixPos, lastWindowsPos);
    
public static booleanisExtension(java.lang.String filename, java.lang.String extension)
Checks whether the extension of the filename is that specified.

This method obtains the extension as the textual part of the filename after the last dot. There must be no directory separator after the dot. The extension check is case-sensitive on all platforms.

param
filename the filename to query, null returns false
param
extension the extension to check for, null or empty checks for no extension
return
true if the filename has the specified extension

        if (filename == null) {
            return false;
        }
        if (extension == null || extension.length() == 0) {
            return (indexOfExtension(filename) == -1);
        }
        String fileExt = getExtension(filename);
        return fileExt.equals(extension);
    
public static booleanisExtension(java.lang.String filename, java.lang.String[] extensions)
Checks whether the extension of the filename is one of those specified.

This method obtains the extension as the textual part of the filename after the last dot. There must be no directory separator after the dot. The extension check is case-sensitive on all platforms.

param
filename the filename to query, null returns false
param
extensions the extensions to check for, null checks for no extension
return
true if the filename is one of the extensions

        if (filename == null) {
            return false;
        }
        if (extensions == null || extensions.length == 0) {
            return (indexOfExtension(filename) == -1);
        }
        String fileExt = getExtension(filename);
        for (int i = 0; i < extensions.length; i++) {
            if (fileExt.equals(extensions[i])) {
                return true;
            }
        }
        return false;
    
public static booleanisExtension(java.lang.String filename, java.util.Collection extensions)
Checks whether the extension of the filename is one of those specified.

This method obtains the extension as the textual part of the filename after the last dot. There must be no directory separator after the dot. The extension check is case-sensitive on all platforms.

param
filename the filename to query, null returns false
param
extensions the extensions to check for, null checks for no extension
return
true if the filename is one of the extensions

        if (filename == null) {
            return false;
        }
        if (extensions == null || extensions.isEmpty()) {
            return (indexOfExtension(filename) == -1);
        }
        String fileExt = getExtension(filename);
        for (Iterator it = extensions.iterator(); it.hasNext();) {
            if (fileExt.equals(it.next())) {
                return true;
            }
        }
        return false;
    
private static booleanisSeparator(char ch)
Checks if the character is a separator.

param
ch the character to check
return
true if it is a separator character

        return (ch == UNIX_SEPARATOR) || (ch == WINDOWS_SEPARATOR);
    
static booleanisSystemWindows()
Determines if Windows file system is in use.

return
true if the system is Windows

        return SYSTEM_SEPARATOR == WINDOWS_SEPARATOR;
    
public static java.lang.Stringnormalize(java.lang.String filename)
Normalizes a path, removing double and single dot path steps.

This method normalizes a path to a standard format. The input may contain separators in either Unix or Windows format. The output will contain separators in the format of the system.

A trailing slash will be retained. A double slash will be merged to a single slash (but UNC names are handled). A single dot path segment will be removed. A double dot will cause that path segment and the one before to be removed. If the double dot has no parent path segment to work with, null is returned.

The output will be the same on both Unix and Windows except for the separator character.

/foo// --> /foo/
/foo/./ --> /foo/
/foo/../bar --> /bar
/foo/../bar/ --> /bar/
/foo/../bar/../baz --> /baz
//foo//./bar --> /foo/bar
/../ --> null
../foo --> null
foo/bar/.. --> foo/
foo/../../bar --> null
foo/../bar --> bar
//server/foo/../bar --> //server/bar
//server/../bar --> null
C:\foo\..\bar --> C:\bar
C:\..\bar --> null
~/foo/../bar/ --> ~/bar/
~/../bar --> null
(Note the file separator returned will be correct for Windows/Unix)

param
filename the filename to normalize, null returns null
return
the normalized filename, or null if invalid

        return doNormalize(filename, true);
    
public static java.lang.StringnormalizeNoEndSeparator(java.lang.String filename)
Normalizes a path, removing double and single dot path steps, and removing any final directory separator.

This method normalizes a path to a standard format. The input may contain separators in either Unix or Windows format. The output will contain separators in the format of the system.

A trailing slash will be removed. A double slash will be merged to a single slash (but UNC names are handled). A single dot path segment will be removed. A double dot will cause that path segment and the one before to be removed. If the double dot has no parent path segment to work with, null is returned.

The output will be the same on both Unix and Windows except for the separator character.

/foo// --> /foo
/foo/./ --> /foo
/foo/../bar --> /bar
/foo/../bar/ --> /bar
/foo/../bar/../baz --> /baz
//foo//./bar --> /foo/bar
/../ --> null
../foo --> null
foo/bar/.. --> foo
foo/../../bar --> null
foo/../bar --> bar
//server/foo/../bar --> //server/bar
//server/../bar --> null
C:\foo\..\bar --> C:\bar
C:\..\bar --> null
~/foo/../bar/ --> ~/bar
~/../bar --> null
(Note the file separator returned will be correct for Windows/Unix)

param
filename the filename to normalize, null returns null
return
the normalized filename, or null if invalid

        return doNormalize(filename, false);
    
public static java.lang.StringremoveExtension(java.lang.String filename)
Removes the extension from a filename.

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

foo.txt --> foo
a\b\c.jpg --> a\b\c
a\b\c --> a\b\c
a.b\c --> a.b\c

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

param
filename the filename to query, null returns null
return
the filename minus the extension

        if (filename == null) {
            return null;
        }
        int index = indexOfExtension(filename);
        if (index == -1) {
            return filename;
        } else {
            return filename.substring(0, index);
        }
    
public static java.lang.StringseparatorsToSystem(java.lang.String path)
Converts all separators to the system separator.

param
path the path to be changed, null ignored
return
the updated path

        if (path == null) {
            return null;
        }
        if (isSystemWindows()) {
            return separatorsToWindows(path);
        } else {
            return separatorsToUnix(path);
        }
    
public static java.lang.StringseparatorsToUnix(java.lang.String path)
Converts all separators to the Unix separator of forward slash.

param
path the path to be changed, null ignored
return
the updated path

        if (path == null || path.indexOf(WINDOWS_SEPARATOR) == -1) {
            return path;
        }
        return path.replace(WINDOWS_SEPARATOR, UNIX_SEPARATOR);
    
public static java.lang.StringseparatorsToWindows(java.lang.String path)
Converts all separators to the Windows separator of backslash.

param
path the path to be changed, null ignored
return
the updated path

        if (path == null || path.indexOf(UNIX_SEPARATOR) == -1) {
            return path;
        }
        return path.replace(UNIX_SEPARATOR, WINDOWS_SEPARATOR);
    
static java.lang.String[]splitOnTokens(java.lang.String text)
Splits a string into a number of tokens.

param
text the text to split
return
the tokens, never null

        // used by wildcardMatch
        // package level so a unit test may run on this
        
        if (text.indexOf("?") == -1 && text.indexOf("*") == -1) {
            return new String[] { text };
        }

        char[] array = text.toCharArray();
        ArrayList list = new ArrayList();
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < array.length; i++) {
            if (array[i] == '?" || array[i] == '*") {
                if (buffer.length() != 0) {
                    list.add(buffer.toString());
                    buffer.setLength(0);
                }
                if (array[i] == '?") {
                    list.add("?");
                } else if (list.size() == 0 ||
                        (i > 0 && list.get(list.size() - 1).equals("*") == false)) {
                    list.add("*");
                }
            } else {
                buffer.append(array[i]);
            }
        }
        if (buffer.length() != 0) {
            list.add(buffer.toString());
        }

        return (String[]) list.toArray( new String[ list.size() ] );
    
public static booleanwildcardMatch(java.lang.String filename, java.lang.String wildcardMatcher)
Checks a filename to see if it matches the specified wildcard matcher, always testing case-sensitive.

The wildcard matcher uses the characters '?' and '*' to represent a single or multiple wildcard characters. This is the same as often found on Dos/Unix command lines. The check is case-sensitive always.

wildcardMatch("c.txt", "*.txt") --> true
wildcardMatch("c.txt", "*.jpg") --> false
wildcardMatch("a/b/c.txt", "a/b/*") --> true
wildcardMatch("c.txt", "*.???") --> true
wildcardMatch("c.txt", "*.????") --> false

param
filename the filename to match on
param
wildcardMatcher the wildcard string to match against
return
true if the filename matches the wilcard string
see
IOCase#SENSITIVE

        return wildcardMatch(filename, wildcardMatcher, IOCase.SENSITIVE);
    
public static booleanwildcardMatch(java.lang.String filename, java.lang.String wildcardMatcher, org.apache.commons.io.IOCase caseSensitivity)
Checks a filename to see if it matches the specified wildcard matcher allowing control over case-sensitivity.

The wildcard matcher uses the characters '?' and '*' to represent a single or multiple wildcard characters.

param
filename the filename to match on
param
wildcardMatcher the wildcard string to match against
param
caseSensitivity what case sensitivity rule to use, null means case-sensitive
return
true if the filename matches the wilcard string
since
Commons IO 1.3

        if (filename == null && wildcardMatcher == null) {
            return true;
        }
        if (filename == null || wildcardMatcher == null) {
            return false;
        }
        if (caseSensitivity == null) {
            caseSensitivity = IOCase.SENSITIVE;
        }
        filename = caseSensitivity.convertCase(filename);
        wildcardMatcher = caseSensitivity.convertCase(wildcardMatcher);
        String[] wcs = splitOnTokens(wildcardMatcher);
        boolean anyChars = false;
        int textIdx = 0;
        int wcsIdx = 0;
        Stack backtrack = new Stack();
        
        // loop around a backtrack stack, to handle complex * matching
        do {
            if (backtrack.size() > 0) {
                int[] array = (int[]) backtrack.pop();
                wcsIdx = array[0];
                textIdx = array[1];
                anyChars = true;
            }
            
            // loop whilst tokens and text left to process
            while (wcsIdx < wcs.length) {
      
                if (wcs[wcsIdx].equals("?")) {
                    // ? so move to next text char
                    textIdx++;
                    anyChars = false;
                    
                } else if (wcs[wcsIdx].equals("*")) {
                    // set any chars status
                    anyChars = true;
                    if (wcsIdx == wcs.length - 1) {
                        textIdx = filename.length();
                    }
                    
                } else {
                    // matching text token
                    if (anyChars) {
                        // any chars then try to locate text token
                        textIdx = filename.indexOf(wcs[wcsIdx], textIdx);
                        if (textIdx == -1) {
                            // token not found
                            break;
                        }
                        int repeat = filename.indexOf(wcs[wcsIdx], textIdx + 1);
                        if (repeat >= 0) {
                            backtrack.push(new int[] {wcsIdx, repeat});
                        }
                    } else {
                        // matching from current position
                        if (!filename.startsWith(wcs[wcsIdx], textIdx)) {
                            // couldnt match token
                            break;
                        }
                    }
      
                    // matched text token, move text index to end of matched token
                    textIdx += wcs[wcsIdx].length();
                    anyChars = false;
                }
      
                wcsIdx++;
            }
            
            // full match
            if (wcsIdx == wcs.length && textIdx == filename.length()) {
                return true;
            }
            
        } while (backtrack.size() > 0);
  
        return false;
    
public static booleanwildcardMatchOnSystem(java.lang.String filename, java.lang.String wildcardMatcher)
Checks a filename to see if it matches the specified wildcard matcher using the case rules of the system.

The wildcard matcher uses the characters '?' and '*' to represent a single or multiple wildcard characters. This is the same as often found on Dos/Unix command lines. The check is case-sensitive on Unix and case-insensitive on Windows.

wildcardMatch("c.txt", "*.txt") --> true
wildcardMatch("c.txt", "*.jpg") --> false
wildcardMatch("a/b/c.txt", "a/b/*") --> true
wildcardMatch("c.txt", "*.???") --> true
wildcardMatch("c.txt", "*.????") --> false

param
filename the filename to match on
param
wildcardMatcher the wildcard string to match against
return
true if the filename matches the wilcard string
see
IOCase#SYSTEM

        return wildcardMatch(filename, wildcardMatcher, IOCase.SYSTEM);