public class FileNameTools extends Object
(例子 C:\dev\project\file.txt):
Modifier and Type | Field and Description |
---|---|
static char |
EXTENSION_SEPARATOR
扩展名与文件名隔离的标点字符
The extension separator character.
|
static String |
EXTENSION_SEPARATOR_STR
扩展名与文件名隔离的标点字符串
The extension separator String.
|
Constructor and Description |
---|
FileNameTools()
Instances should NOT be constructed in standard programming.
|
Modifier and Type | Method and Description |
---|---|
static String |
concat(String basePath,
String fullFilenameToAdd)
组合生成完整文件路径
Concatenates a filename to a base path using normal command line style rules.
|
static boolean |
directoryContains(String canonicalParent,
String canonicalChild)
Determines whether the
parent directory contains the child element (a file or directory). |
static boolean |
equals(String filename1,
String filename2)
Checks whether two filenames are equal exactly.
|
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.
|
static boolean |
equalsNormalized(String filename1,
String filename2)
Checks whether two filenames are equal after both have been normalized.
|
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.
|
static boolean |
equalsOnSystem(String filename1,
String filename2)
Checks whether two filenames are equal using the case rules of the system.
|
static String |
getBaseName(String filename)
获取文件名
Gets the base name, minus the full path and extension, from a full filename.
|
static String |
getExtension(String filename)
获取文件扩展名
Gets the extension of a filename.
|
static String |
getFullPath(String filename)
获取文件所在路径的全路径
Gets the full path from a full filename, which is the prefix + path.
|
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.
|
static Map<String,String> |
getInfoFromFileName(String filename)
从文件名中提取信息,须结合 top.hmtools.io.FileNameTools.saveInifo2FileName(Map
|
static String |
getName(String filename)
获取全文件名,包括扩展名
Gets the name minus the path from a full filename.
|
static String |
getOriginalNameByInfoName(String filename)
从加入信息后的文件名中提取原有文件名,须结合 top.hmtools.io.FileNameTools.saveInifo2FileName(Map
|
static String |
getPath(String filename)
Gets the path from a full filename, which excludes the prefix.
|
static String |
getPathNoEndSeparator(String filename)
Gets the path from a full filename, which excludes the prefix, and
also excluding the final directory separator.
|
static String |
getPrefix(String filename)
Gets the prefix from a full filename, such as
C:/
or ~/ . |
static int |
getPrefixLength(String filename)
Returns the length of the filename prefix, such as
C:/ or ~/ . |
static int |
indexOfExtension(String filename)
Returns the index of the last extension separator character, which is a dot.
|
static int |
indexOfLastSeparator(String filename)
Returns the index of the last directory separator character.
|
static boolean |
isExtension(String filename,
Collection<String> extensions)
Checks whether the extension of the filename is one of those specified.
|
static boolean |
isExtension(String filename,
String extension)
Checks whether the extension of the filename is that specified.
|
static boolean |
isExtension(String filename,
String[] extensions)
Checks whether the extension of the filename is one of those specified.
|
static String |
normalize(String filename)
Normalizes a path, removing double and single dot path steps.
|
static String |
normalize(String filename,
boolean unixSeparator)
Normalizes a path, removing double and single dot path steps.
|
static String |
normalizeNoEndSeparator(String filename)
整理文件路径,使其“标准”化。比如去除重复多余的路径分隔符
Normalizes a path, removing double and single dot path steps,
and removing any final directory separator.
|
static String |
normalizeNoEndSeparator(String filename,
boolean unixSeparator)
整理文件路径,使其“标准”化。比如去除重复多余的路径分隔符
Normalizes a path, removing double and single dot path steps,
and removing any final directory separator.
|
static String |
removeExtension(String filename)
Removes the extension from a filename.
|
static String |
saveInifo2FileName(Map<String,String> info,
String filename)
将信息插入到文件名中
|
static String |
separatorsToSystem(String path)
改文件路径为当前系统文件路径
Converts all separators to the system separator.
|
static String |
separatorsToUnix(String path)
将Windows文件路径改为Unix文件路径
Converts all separators to the Unix separator of forward slash.
|
static String |
separatorsToWindows(String path)
将Unix文件路径改为Windows文件路径
Converts all separators to the Windows separator of backslash.
|
static boolean |
wildcardMatch(String filename,
String wildcardMatcher)
Checks a filename to see if it matches the specified wildcard matcher,
always testing case-sensitive.
|
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.
|
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.
|
public static final char EXTENSION_SEPARATOR
public static final String EXTENSION_SEPARATOR_STR
public FileNameTools()
public static String normalize(String filename)
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)
filename
- the filename to normalize, null returns nullpublic static String normalize(String filename, boolean unixSeparator)
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 specified.
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 --> nullThe output will be the same on both Unix and Windows including the separator character.
filename
- the filename to normalize, null returns nullunixSeparator
- true
if a unix separator should
be used or false
if a windows separator should be used.public static String normalizeNoEndSeparator(String filename)
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)
filename
- the filename to normalize, null returns nullpublic static String normalizeNoEndSeparator(String filename, boolean unixSeparator)
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 specified.
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 including 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
filename
- the filename to normalize, null returns nullunixSeparator
- true
if a unix separator should
be used or false
if a windows separtor should be used.public static String concat(String basePath, String fullFilenameToAdd)
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.basePath
- the base path to attach to, always treated as a pathfullFilenameToAdd
- the filename (or path) to attach to the basepublic static boolean directoryContains(String canonicalParent, String canonicalChild) throws IOException
parent
directory contains the child
element (a file or directory).
The files names are expected to be normalized.
Edge cases:directory
must not be null: if null, throw IllegalArgumentExceptioncanonicalParent
- the file to consider as the parent.canonicalChild
- the file to consider as the child.IOException
- if an IO error occurs while checking the files.FileUtils#directoryContains(File, File)
public static String separatorsToUnix(String path)
path
- the path to be changed, null ignoredpublic static String separatorsToWindows(String path)
path
- the path to be changed, null ignoredpublic static String separatorsToSystem(String path)
path
- the path to be changed, null ignoredpublic static int getPrefixLength(String filename)
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.
filename
- the filename to find the prefix in, null returns -1public static int indexOfLastSeparator(String filename)
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.
filename
- the filename to find the last path separator in, null returns -1public static int indexOfExtension(String filename)
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.
filename
- the filename to find the last path separator in, null returns -1public static String getPrefix(String filename)
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.
filename
- the filename to query, null returns nullpublic static String getPath(String filename)
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.
filename
- the filename to query, null returns nullpublic static String getPathNoEndSeparator(String filename)
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.
filename
- the filename to query, null returns nullpublic static String getFullPath(String filename)
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.
filename
- the filename to query, null returns nullpublic static String getFullPathNoEndSeparator(String filename)
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.
filename
- the filename to query, null returns nullpublic static String getName(String 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.
filename
- the filename to query, null returns nullpublic static String getBaseName(String 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.
filename
- the filename to query, null returns nullpublic static String getExtension(String 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.
filename
- the filename to retrieve the extension of.null
if the filename is null
.public static String removeExtension(String 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.
filename
- the filename to query, null returns nullpublic static boolean equals(String filename1, String filename2)
No processing is performed on the filenames other than comparison, thus this is merely a null-safe case-sensitive equals.
filename1
- the first filename to query, may be nullfilename2
- the second filename to query, may be nullIOCase.SENSITIVE
public static boolean equalsOnSystem(String filename1, String filename2)
No processing is performed on the filenames other than comparison. The check is case-sensitive on Unix and case-insensitive on Windows.
filename1
- the first filename to query, may be nullfilename2
- the second filename to query, may be nullIOCase.SYSTEM
public static boolean equalsNormalized(String filename1, String filename2)
Both filenames are first passed to normalize(String)
.
The check is then performed in a case-sensitive manner.
filename1
- the first filename to query, may be nullfilename2
- the second filename to query, may be nullIOCase.SENSITIVE
public static boolean equalsNormalizedOnSystem(String filename1, String filename2)
Both filenames are first passed to normalize(String)
.
The check is then performed case-sensitive on Unix and
case-insensitive on Windows.
filename1
- the first filename to query, may be nullfilename2
- the second filename to query, may be nullIOCase.SYSTEM
public static boolean equals(String filename1, String filename2, boolean normalized, IOCase caseSensitivity)
filename1
- the first filename to query, may be nullfilename2
- the second filename to query, may be nullnormalized
- whether to normalize the filenamescaseSensitivity
- what case sensitivity rule to use, null means case-sensitivepublic static boolean isExtension(String filename, String extension)
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.
filename
- the filename to query, null returns falseextension
- the extension to check for, null or empty checks for no extensionpublic static boolean isExtension(String filename, String[] extensions)
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.
filename
- the filename to query, null returns falseextensions
- the extensions to check for, null checks for no extensionpublic static boolean isExtension(String filename, Collection<String> extensions)
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.
filename
- the filename to query, null returns falseextensions
- the extensions to check for, null checks for no extensionpublic static boolean wildcardMatch(String filename, String wildcardMatcher)
The wildcard matcher uses the characters '?' and '*' to represent a single or multiple (zero or more) 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", "*.????") --> falseN.B. the sequence "*?" does not work properly at present in match strings.
filename
- the filename to match onwildcardMatcher
- the wildcard string to match againstIOCase.SENSITIVE
public static boolean wildcardMatchOnSystem(String filename, String wildcardMatcher)
The wildcard matcher uses the characters '?' and '*' to represent a single or multiple (zero or more) 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", "*.????") --> falseN.B. the sequence "*?" does not work properly at present in match strings.
filename
- the filename to match onwildcardMatcher
- the wildcard string to match againstIOCase.SYSTEM
public static boolean wildcardMatch(String filename, String wildcardMatcher, IOCase caseSensitivity)
The wildcard matcher uses the characters '?' and '*' to represent a single or multiple (zero or more) wildcard characters. N.B. the sequence "*?" does not work properly at present in match strings.
filename
- the filename to match onwildcardMatcher
- the wildcard string to match againstcaseSensitivity
- what case sensitivity rule to use, null means case-sensitivepublic static String saveInifo2FileName(Map<String,String> info, String filename)
info
- filename
- public static Map<String,String> getInfoFromFileName(String filename)
filename
- Copyright © 2018. All rights reserved.