Top Description Fields Constructors Methods
org.apache.commons.io

public Class FilenameUtils

extends Object
Class Inheritance
Imports
java.io.File, java.util.ArrayList, .Collection, .Iterator, .Stack

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 File objects and the two argument constructor 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):

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.

Authors
Kevin A. Burton, Scott Sanders, Daniel Rall, Christoph.Reck, Peter Donald, Jeff Turner, Matthew Hawthorne, Martin Cooper, Jeremias Maerki, Stephen Colebourne
Since
Commons IO 1.1
Version
$Id: FilenameUtils.java 490424 2006-12-27 01:20:43Z bayard $

Field Summary

Modifier and TypeField and Description
private static final char
EXTENSION_SEPARATOR

The extension separator character.

private static final char
OTHER_SEPARATOR

The separator character that is the opposite of the system separator.

private static final char
SYSTEM_SEPARATOR

The system separator character.

private static final char
UNIX_SEPARATOR

The Unix separator character.

private static final char
WINDOWS_SEPARATOR

The Windows separator character.

Constructor Summary

AccessConstructor and Description
public
FilenameUtils()

Instances should NOT be constructed in standard programming.

Method Summary

Modifier and TypeMethod and Description
public static String

Returns:

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

Concatenates a filename to a base path using normal command line style rules.

private static String

Returns:

the path
doGetFullPath
(String
the filename
filename
,
boolean
true to include the end separator
includeSeparator
)

Does the work of getting the path.

private static String

Returns:

the path
doGetPath
(String
the filename
filename
,
int
0 to omit the end separator, 1 to return it
separatorAdd
)

Does the work of getting the path.

private static String

Returns:

the normalized filename
doNormalize
(String
the filename
filename
,
boolean
true to keep the final separator
keepSeparator
)

Internal method to perform the normalization.

public static boolean

Returns:

true if the filenames are equal, null equals null
equals
(String
the first filename to query, may be null
filename1
,
String
the second filename to query, may be null
filename2
)

Checks whether two filenames are equal exactly.

public static boolean

Returns:

true if the filenames are equal, null equals null
equals
(String
the first filename to query, may be null
filename1
,
String
the second filename to query, may be null
filename2
,
boolean
whether to normalize the filenames
normalized
,
IOCase
what case sensitivity rule to use, null means case-sensitive
caseSensitivity
)

Checks whether two filenames are equal, optionally normalizing and providing control over the case-sensitivity.

public static boolean

Returns:

true if the filenames are equal, null equals null
equalsNormalized
(String
the first filename to query, may be null
filename1
,
String
the second filename to query, may be null
filename2
)

Checks whether two filenames are equal after both have been normalized.

public static boolean

Returns:

true if the filenames are equal, null equals null
equalsNormalizedOnSystem
(String
the first filename to query, may be null
filename1
,
String
the second filename to query, may be null
filename2
)

Checks whether two filenames are equal after both have been normalized and using the case rules of the system.

public static boolean

Returns:

true if the filenames are equal, null equals null
equalsOnSystem
(String
the first filename to query, may be null
filename1
,
String
the second filename to query, may be null
filename2
)

Checks whether two filenames are equal using the case rules of the system.

public static String

Returns:

the name of the file without the path, or an empty string if none exists
getBaseName
(String
the filename to query, null returns null
filename
)

Gets the base name, minus the full path and extension, from a full filename.

public static String

Returns:

the extension of the file or an empty string if none exists.
getExtension
(String
the filename to retrieve the extension of.
filename
)

Gets the extension of a filename.

public static String

Returns:

the path of the file, an empty string if none exists, null if invalid
getFullPath
(String
the filename to query, null returns null
filename
)

Gets the full path from a full filename, which is the prefix + path.

public static String

Returns:

the path of the file, an empty string if none exists, null if invalid
getFullPathNoEndSeparator
(String
the filename to query, null returns null
filename
)

Gets the full path from a full filename, which is the prefix + path, and also excluding the final directory separator.

public static String

Returns:

the name of the file without the path, or an empty string if none exists
getName
(String
the filename to query, null returns null
filename
)

Gets the name minus the path from a full filename.

public static String

Returns:

the path of the file, an empty string if none exists, null if invalid
getPath
(String
the filename to query, null returns null
filename
)

Gets the path from a full filename, which excludes the prefix.

public static String

Returns:

the path of the file, an empty string if none exists, null if invalid
getPathNoEndSeparator
(String
the filename to query, null returns null
filename
)

Gets the path from a full filename, which excludes the prefix, and also excluding the final directory separator.

public static String

Returns:

the prefix of the file, null if invalid
getPrefix
(String
the filename to query, null returns null
filename
)

Gets the prefix from a full filename, such as C:/ or ~/.

public static int

Returns:

the length of the prefix, -1 if invalid or null
getPrefixLength
(String
the filename to find the prefix in, null returns -1
filename
)

Returns the length of the filename prefix, such as C:/ or ~/.

public static int

Returns:

the index of the last separator character, or -1 if there is no such character
indexOfExtension
(String
the filename to find the last path separator in, null returns -1
filename
)

Returns the index of the last extension separator character, which is a dot.

public static int

Returns:

the index of the last separator character, or -1 if there is no such character
indexOfLastSeparator
(String
the filename to find the last path separator in, null returns -1
filename
)

Returns the index of the last directory separator character.

public static boolean

Returns:

true if the filename has the specified extension
isExtension
(String
the filename to query, null returns false
filename
,
String
the extension to check for, null or empty checks for no extension
extension
)

Checks whether the extension of the filename is that specified.

public static boolean

Returns:

true if the filename is one of the extensions
isExtension
(String
the filename to query, null returns false
filename
,
String[]
the extensions to check for, null checks for no extension
extensions
)

Checks whether the extension of the filename is one of those specified.

public static boolean

Returns:

true if the filename is one of the extensions
isExtension
(String
the filename to query, null returns false
filename
,
Collection<E>
the extensions to check for, null checks for no extension
extensions
)

Checks whether the extension of the filename is one of those specified.

private static boolean

Returns:

true if it is a separator character
isSeparator
(char
the character to check
ch
)

Checks if the character is a separator.

pack-priv static boolean

Returns:

true if the system is Windows
isSystemWindows
()

Determines if Windows file system is in use.

public static String

Returns:

the normalized filename, or null if invalid
normalize
(String
the filename to normalize, null returns null
filename
)

Normalizes a path, removing double and single dot path steps.

public static String

Returns:

the normalized filename, or null if invalid
normalizeNoEndSeparator
(String
the filename to normalize, null returns null
filename
)

Normalizes a path, removing double and single dot path steps, and removing any final directory separator.

public static String

Returns:

the filename minus the extension
removeExtension
(String
the filename to query, null returns null
filename
)

Removes the extension from a filename.

public static String

Returns:

the updated path
separatorsToSystem
(String
the path to be changed, null ignored
path
)

Converts all separators to the system separator.

public static String

Returns:

the updated path
separatorsToUnix
(String
the path to be changed, null ignored
path
)

Converts all separators to the Unix separator of forward slash.

public static String

Returns:

the updated path
separatorsToWindows
(String
the path to be changed, null ignored
path
)

Converts all separators to the Windows separator of backslash.

pack-priv static String[]

Returns:

the tokens, never null
splitOnTokens
(String
the text to split
text
)

Splits a string into a number of tokens.

public static boolean

Returns:

true if the filename matches the wilcard string
wildcardMatch
(String
the filename to match on
filename
,
String
the wildcard string to match against
wildcardMatcher
)

Checks a filename to see if it matches the specified wildcard matcher, always testing case-sensitive.

public static boolean

Returns:

true if the filename matches the wilcard string
wildcardMatch
(String
the filename to match on
filename
,
String
the wildcard string to match against
wildcardMatcher
,
IOCase
what case sensitivity rule to use, null means case-sensitive
caseSensitivity
)

Checks a filename to see if it matches the specified wildcard matcher allowing control over case-sensitivity.

public static boolean

Returns:

true if the filename matches the wilcard string
wildcardMatchOnSystem
(String
the filename to match on
filename
,
String
the wildcard string to match against
wildcardMatcher
)

Checks a filename to see if it matches the specified wildcard matcher using the case rules of the system.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

EXTENSION_SEPARATORback to summary
private static final char EXTENSION_SEPARATOR

The extension separator character.

OTHER_SEPARATORback to summary
private static final char OTHER_SEPARATOR

The separator character that is the opposite of the system separator.

SYSTEM_SEPARATORback to summary
private static final char SYSTEM_SEPARATOR

The system separator character.

UNIX_SEPARATORback to summary
private static final char UNIX_SEPARATOR

The Unix separator character.

WINDOWS_SEPARATORback to summary
private static final char WINDOWS_SEPARATOR

The Windows separator character.

Constructor Detail

FilenameUtilsback to summary
public FilenameUtils()

Instances should NOT be constructed in standard programming.

Method Detail

concatback to summary
public static String concat(String basePath, 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 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 getFullPath(String) on the base path argument.
Parameters
basePath:String

the base path to attach to, always treated as a path

fullFilenameToAdd:String

the filename (or path) to attach to the base

Returns:String

the concatenated path, or null if invalid

doGetFullPathback to summary
private static String doGetFullPath(String filename, boolean includeSeparator)

Does the work of getting the path.

Parameters
filename:String

the filename

includeSeparator:boolean

true to include the end separator

Returns:String

the path

doGetPathback to summary
private static String doGetPath(String filename, int separatorAdd)

Does the work of getting the path.

Parameters
filename:String

the filename

separatorAdd:int

0 to omit the end separator, 1 to return it

Returns:String

the path

doNormalizeback to summary
private static String doNormalize(String filename, boolean keepSeparator)

Internal method to perform the normalization.

Parameters
filename:String

the filename

keepSeparator:boolean

true to keep the final separator

Returns:String

the normalized filename

equalsback to summary
public static boolean equals(String filename1, 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.

Parameters
filename1:String

the first filename to query, may be null

filename2:String

the second filename to query, may be null

Returns:boolean

true if the filenames are equal, null equals null

See Also
IOCase#SENSITIVE
equalsback to summary
public static boolean equals(String filename1, String filename2, boolean normalized, IOCase caseSensitivity)

Checks whether two filenames are equal, optionally normalizing and providing control over the case-sensitivity.

Parameters
filename1:String

the first filename to query, may be null

filename2:String

the second filename to query, may be null

normalized:boolean

whether to normalize the filenames

caseSensitivity:IOCase

what case sensitivity rule to use, null means case-sensitive

Returns:boolean

true if the filenames are equal, null equals null

Since
Commons IO 1.3
equalsNormalizedback to summary
public static boolean equalsNormalized(String filename1, String filename2)

Checks whether two filenames are equal after both have been normalized.

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

Parameters
filename1:String

the first filename to query, may be null

filename2:String

the second filename to query, may be null

Returns:boolean

true if the filenames are equal, null equals null

See Also
IOCase#SENSITIVE
equalsNormalizedOnSystemback to summary
public static boolean equalsNormalizedOnSystem(String filename1, 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 normalize(String). The check is then performed case-sensitive on Unix and case-insensitive on Windows.

Parameters
filename1:String

the first filename to query, may be null

filename2:String

the second filename to query, may be null

Returns:boolean

true if the filenames are equal, null equals null

See Also
IOCase#SYSTEM
equalsOnSystemback to summary
public static boolean equalsOnSystem(String filename1, 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.

Parameters
filename1:String

the first filename to query, may be null

filename2:String

the second filename to query, may be null

Returns:boolean

true if the filenames are equal, null equals null

See Also
IOCase#SYSTEM
getBaseNameback to summary
public static String getBaseName(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.

Parameters
filename:String

the filename to query, null returns null

Returns:String

the name of the file without the path, or an empty string if none exists

getExtensionback to summary
public static String getExtension(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:String

the filename to retrieve the extension of.

Returns:String

the extension of the file or an empty string if none exists.

getFullPathback to summary
public static String getFullPath(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.

Parameters
filename:String

the filename to query, null returns null

Returns:String

the path of the file, an empty string if none exists, null if invalid

getFullPathNoEndSeparatorback to summary
public static String getFullPathNoEndSeparator(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.

Parameters
filename:String

the filename to query, null returns null

Returns:String

the path of the file, an empty string if none exists, null if invalid

getNameback to summary
public static String getName(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:String

the filename to query, null returns null

Returns:String

the name of the file without the path, or an empty string if none exists

getPathback to summary
public static String getPath(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 getFullPath(String) for the method that retains the prefix.

Parameters
filename:String

the filename to query, null returns null

Returns:String

the path of the file, an empty string if none exists, null if invalid

getPathNoEndSeparatorback to summary
public static String getPathNoEndSeparator(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 getFullPathNoEndSeparator(String) for the method that retains the prefix.

Parameters
filename:String

the filename to query, null returns null

Returns:String

the path of the file, an empty string if none exists, null if invalid

getPrefixback to summary
public static String getPrefix(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.

Parameters
filename:String

the filename to query, null returns null

Returns:String

the prefix of the file, null if invalid

getPrefixLengthback to summary
public static int getPrefixLength(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.

Parameters
filename:String

the filename to find the prefix in, null returns -1

Returns:int

the length of the prefix, -1 if invalid or null

indexOfExtensionback to summary
public static int indexOfExtension(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:String

the filename to find the last path separator in, null returns -1

Returns:int

the index of the last separator character, or -1 if there is no such character

indexOfLastSeparatorback to summary
public static int indexOfLastSeparator(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:String

the filename to find the last path separator in, null returns -1

Returns:int

the index of the last separator character, or -1 if there is no such character

isExtensionback to summary
public static boolean isExtension(String filename, 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.

Parameters
filename:String

the filename to query, null returns false

extension:String

the extension to check for, null or empty checks for no extension

Returns:boolean

true if the filename has the specified extension

isExtensionback to summary
public static boolean isExtension(String filename, 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.

Parameters
filename:String

the filename to query, null returns false

extensions:String[]

the extensions to check for, null checks for no extension

Returns:boolean

true if the filename is one of the extensions

isExtensionback to summary
public static boolean isExtension(String filename, Collection<E> 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.

Parameters
filename:String

the filename to query, null returns false

extensions:Collection<E>

the extensions to check for, null checks for no extension

Returns:boolean

true if the filename is one of the extensions

isSeparatorback to summary
private static boolean isSeparator(char ch)

Checks if the character is a separator.

Parameters
ch:char

the character to check

Returns:boolean

true if it is a separator character

isSystemWindowsback to summary
pack-priv static boolean isSystemWindows()

Determines if Windows file system is in use.

Returns:boolean

true if the system is Windows

normalizeback to summary
public static String normalize(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)
Parameters
filename:String

the filename to normalize, null returns null

Returns:String

the normalized filename, or null if invalid

normalizeNoEndSeparatorback to summary
public static String normalizeNoEndSeparator(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)
Parameters
filename:String

the filename to normalize, null returns null

Returns:String

the normalized filename, or null if invalid

removeExtensionback to summary
public static String removeExtension(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.

Parameters
filename:String

the filename to query, null returns null

Returns:String

the filename minus the extension

separatorsToSystemback to summary
public static String separatorsToSystem(String path)

Converts all separators to the system separator.

Parameters
path:String

the path to be changed, null ignored

Returns:String

the updated path

separatorsToUnixback to summary
public static String separatorsToUnix(String path)

Converts all separators to the Unix separator of forward slash.

Parameters
path:String

the path to be changed, null ignored

Returns:String

the updated path

separatorsToWindowsback to summary
public static String separatorsToWindows(String path)

Converts all separators to the Windows separator of backslash.

Parameters
path:String

the path to be changed, null ignored

Returns:String

the updated path

splitOnTokensback to summary
pack-priv static String[] splitOnTokens(String text)

Splits a string into a number of tokens.

Parameters
text:String

the text to split

Returns:String[]

the tokens, never null

wildcardMatchback to summary
public static boolean wildcardMatch(String filename, 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
Parameters
filename:String

the filename to match on

wildcardMatcher:String

the wildcard string to match against

Returns:boolean

true if the filename matches the wilcard string

See Also
IOCase#SENSITIVE
wildcardMatchback to summary
public static boolean wildcardMatch(String filename, String wildcardMatcher, 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.

Parameters
filename:String

the filename to match on

wildcardMatcher:String

the wildcard string to match against

caseSensitivity:IOCase

what case sensitivity rule to use, null means case-sensitive

Returns:boolean

true if the filename matches the wilcard string

Since
Commons IO 1.3
wildcardMatchOnSystemback to summary
public static boolean wildcardMatchOnSystem(String filename, 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
Parameters
filename:String

the filename to match on

wildcardMatcher:String

the wildcard string to match against

Returns:boolean

true if the filename matches the wilcard string

See Also
IOCase#SYSTEM