Class FileNameUtil
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):
- the prefix - C:\
- the path - dev\project\
- the full path - C:\dev\project\
- the name - file.txt
- the base name - file
- the extension - txt
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.-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final charThe extension separator character.private static final charThe separator character that is the opposite of the system separator.private static final charThe system separator character.private static final charThe Unix separator character.private static final charThe Windows separator character. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic StringConcatenates a filename to a base path using normal command line style rules.static Stringstatic Stringprivate static StringdoGetFullPath(String filename, boolean includeSeparator) Does the work of getting the path.private static StringDoes the work of getting the path.private static StringdoNormalize(String filename, char separator, boolean keepSeparator) Internal method to perform the normalization.static booleanChecks whether two filenames are equal exactly.private static booleanChecks whether two filenames are equal optionally using the case rules of the system.static booleanequalsOnSystem(String filename1, String filename2) Checks whether two filenames are equal using the case rules of the system.static StringgetBaseName(String filename) Gets the base name, minus the full path and extension, from a full filename.static StringgetExtension(String filename) Gets the extension of a filename.static StringgetFullPath(String filename) Gets the full path from a full filename, which is the prefix + path.static StringgetFullPathNoEndSeparator(String filename) Gets the full path from a full filename, which is the prefix + path, and also excluding the final directory separator.static StringGets the name minus the path from a full filename.static StringGets the path from a full filename, which excludes the prefix.static StringgetPathNoEndSeparator(String filename) Gets the path from a full filename, which excludes the prefix, and also excluding the final directory separator.static StringGets the prefix from a full filename, such asC:/or~/.static intgetPrefixLength(String filename) Returns the length of the filename prefix, such asC:/or~/.static booleanhasExtension(String filename) Returnstrueif file has extension.static intindexOfExtension(String filename) Returns the index of the last extension separator character, which is a dot.static intindexOfLastSeparator(String filename) Returns the index of the last directory separator character.private static booleanisSeparator(char ch) Checks if the character is a separator.static Stringstatic StringNormalizes a path, removing double and single dot path steps.static StringnormalizeNoEndSeparator(String filename) static StringnormalizeNoEndSeparator(String filename, boolean unixSeparator) Normalizes a path, removing double and single dot path steps, and removing any final directory separator.static StringrelativePath(String targetPath, String basePath) Calculates relative path of target path on base path.static StringremoveExtension(String filename) Removes the extension from a filename.static StringresolveHome(String path) Resolve~in the path.static StringseparatorsToSystem(String path) Converts all separators to the system separator.static StringseparatorsToUnix(String path) Converts all separators to the Unix separator of forward slash.static StringseparatorsToWindows(String path) Converts all separators to the Windows separator of backslash.static String[]Splits filename into a array of four Strings containing prefix, path, basename and extension.
-
Field Details
-
EXTENSION_SEPARATOR
private static final char EXTENSION_SEPARATORThe extension separator character.- See Also:
-
UNIX_SEPARATOR
private static final char UNIX_SEPARATORThe Unix separator character.- See Also:
-
WINDOWS_SEPARATOR
private static final char WINDOWS_SEPARATORThe Windows separator character.- See Also:
-
SYSTEM_SEPARATOR
private static final char SYSTEM_SEPARATORThe system separator character. -
OTHER_SEPARATOR
private static final char OTHER_SEPARATORThe separator character that is the opposite of the system separator.
-
-
Constructor Details
-
FileNameUtil
public FileNameUtil()
-
-
Method Details
-
isSeparator
private static boolean isSeparator(char ch) Checks if the character is a separator. -
normalize
-
normalize
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,
nullis returned.The output will be the same on both Unix and Windows except for the separator character.
(Note the file separator returned will be correct for Windows/Unix)/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- Parameters:
filename- the filename to normalize, null returns null- Returns:
- the normalized filename, or null if invalid
-
normalizeNoEndSeparator
-
normalizeNoEndSeparator
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,
nullis returned.The output will be the same on both Unix and Windows except for the separator character.
(Note the file separator returned will be correct for Windows/Unix)/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- Parameters:
filename- the filename to normalize, null returns null- Returns:
- the normalized filename, or null if invalid
-
doNormalize
Internal method to perform the normalization.- Parameters:
filename- file nameseparator- separator character to usekeepSeparator-trueto keep the final separator- Returns:
- normalized filename
-
concat
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
pathToAddis 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.
(*) 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/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 (!)getFullPath(String)on the base path argument.- Parameters:
basePath- the base path to attach to, always treated as a pathfullFilenameToAdd- the filename (or path) to attach to the base- Returns:
- the concatenated path, or null if invalid
-
concat
-
doConcat
-
separatorsToUnix
Converts all separators to the Unix separator of forward slash.- Parameters:
path- the path to be changed, null ignored- Returns:
- the updated path
-
separatorsToWindows
Converts all separators to the Windows separator of backslash.- Parameters:
path- the path to be changed, null ignored- Returns:
- the updated path
-
separatorsToSystem
Converts all separators to the system separator.- Parameters:
path- the path to be changed, null ignored- Returns:
- the updated path
-
getPrefixLength
Returns the length of the filename prefix, such asC:/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- the filename to find the prefix in, null returns -1- Returns:
- the length of the prefix, -1 if invalid or null
-
indexOfLastSeparator
Returns the index of the last directory separator character.This method will handle a file in either Unix or Windows format. The position of the last forward or backslash is returned.
The output will be the same irrespective of the machine that the code is running on.
- Parameters:
filename- the filename to find the last path separator in, null returns -1- Returns:
- the index of the last separator character, or -1 if there is no such character
-
indexOfExtension
Returns the index of the last extension separator character, which is a dot.This method also checks that there is no directory separator after the last dot. To do this it uses
indexOfLastSeparator(String)which will handle a file in either Unix or Windows format.The output will be the same irrespective of the machine that the code is running on.
- Parameters:
filename- the filename to find the last path separator in, null returns -1- Returns:
- the index of the last separator character, or -1 if there is no such character
-
hasExtension
Returnstrueif file has extension. -
getPrefix
Gets the prefix from a full filename, such asC:/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- the filename to query, null returns null- Returns:
- the prefix of the file, null if invalid
-
getPath
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- the filename to query, null returns null- Returns:
- the path of the file, an empty string if none exists, null if invalid
-
getPathNoEndSeparator
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/cThe 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- the filename to query, null returns null- Returns:
- the path of the file, an empty string if none exists, null if invalid
-
doGetPath
Does the work of getting the path.- Parameters:
filename- the filenameseparatorAdd- 0 to omit the end separator, 1 to return it- Returns:
- the path
-
getFullPath
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- the filename to query, null returns null- Returns:
- the path of the file, an empty string if none exists, null if invalid
-
getFullPathNoEndSeparator
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/ --> ~userThe output will be the same irrespective of the machine that the code is running on.
- Parameters:
filename- the filename to query, null returns null- Returns:
- the path of the file, an empty string if none exists, null if invalid
-
doGetFullPath
Does the work of getting the path.- Parameters:
filename- the filenameincludeSeparator- true to include the end separator- Returns:
- the path
-
getName
Gets the name minus the path from a full filename.This method will handle a file in either Unix or Windows format. The text after the last forward or backslash is returned.
a/b/c.txt --> c.txt a.txt --> a.txt a/b/c --> c a/b/c/ --> ""The output will be the same irrespective of the machine that the code is running on.
- Parameters:
filename- the filename to query, null returns null- Returns:
- the name of the file without the path, or an empty string if none exists
-
getBaseName
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- the filename to query, null returns null- Returns:
- the name of the file without the path, or an empty string if none exists
-
getExtension
Gets the extension of a filename.This method returns the textual part of the filename after the last dot. There must be no directory separator after the dot.
foo.txt --> "txt" a/b/c.jpg --> "jpg" a/b.txt/c --> "" a/b/c --> ""The output will be the same irrespective of the machine that the code is running on.
- Parameters:
filename- the filename to retrieve the extension of.- Returns:
- the extension of the file or an empty string if none exists.
-
removeExtension
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\cThe output will be the same irrespective of the machine that the code is running on.
- Parameters:
filename- the filename to query, null returns null- Returns:
- the filename minus the extension
-
equals
Checks whether two filenames are equal exactly. -
equalsOnSystem
Checks whether two filenames are equal using the case rules of the system. -
equals
Checks whether two filenames are equal optionally using the case rules of the system.- Parameters:
filename1- the first filename to query, may be nullfilename2- the second filename to query, may be nullsystem- whether to use the system (windows or unix)- Returns:
- true if the filenames are equal, null equals null
-
split
Splits filename into a array of four Strings containing prefix, path, basename and extension. Path will contain ending separator. -
resolveHome
Resolve~in the path. -
relativePath
Calculates relative path of target path on base path.
-