Package com.helger.commons.io.file
Class FilenameHelper
java.lang.Object
com.helger.commons.io.file.FilenameHelper
All kind of file name handling stuff. This class gives you platform
independent file name handling.
- Author:
- Philip Helger
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final charThe file extension separation character.static final charThe prefix used for Unix hidden filesstatic final charThe replacement character used for illegal file name characters.static final StringSpecial name of the current pathstatic final StringSpecial name of the parent pathstatic final charThe Unix path separator character.static final StringThe Unix path separator string.static final StringThe prefix to identify UNC paths on Unix based systemsstatic final charThe Windows separator character.static final StringThe Windows separator string.static final StringThe prefix to identify UNC paths on Windows based systemsstatic final StringThe prefix to identify local UNC paths on Windows based systems.
Those no longer work in files in certain Java versions.static final StringThe prefix to identify local UNC paths on Windows based systems
Those no longer work in files in certain Java versions. -
Method Summary
Modifier and TypeMethodDescriptionstatic booleanCheck if the passed String contains at least one path separator char (either Windows or Unix style).static booleanCheck if the passed character sequence ends with a path separation character.static StringEnsure that the passed path does NOT end with a directory separator character.static StringEnsure that the passed path ends with a directory separator character.static StringEnsure that the passed path starts with a directory separator character.static StringgetAbsoluteWithEnsuredParentDirectory(File aParentDirectory, String sFilePath) Get a concatenated absolute path consisting of the parent directory and the file path.static StringgetAsSecureValidASCIIFilename(String sFilename) Replace all non-ASCII characters from the filename (e.g.static StringgetAsSecureValidASCIIFilename(String sFilename, char cReplacementChar) Replace all non-ASCII characters from the filename (e.g.static StringgetAsSecureValidFilename(String sFilename) Convert the passed filename into a valid filename by performing the following actions: Remove everything after a potential \0 character Remove all characters that are invalid at the end of a file name Replace all characters that are invalid inside a filename with a underscore If the filename is invalid on Windows platforms it is prefixed with an underscore. Note: this method does not handle Windows full path like "c:\autoexec.bat"static StringgetBaseName(File aFile) Get the passed filename without path and without extension.
Example:/dir1/dir2/file.txtbecomesfilestatic StringgetBaseName(String sAbsoluteFilename) Get the passed filename without path and without extension.
Example:/dir1/dir2/file.txtbecomesfilestatic StringgetCleanConcatenatedUrlPath(String sURL, String sPath) Concatenate a base URL and a sub path incl. the path cleansing.static StringgetCleanPath(File aFile) Get a clean path of the passed file resolving all "." and ".." paths.
Note: in caseFileHelper.getCanonicalPath(File)fails,getCleanPath(String)is used as a fallback.
Note 2: no cleansing operations beside "." and ".." are returned.static StringgetCleanPath(String sPath) Clean the path by removing all ".." and "." path elements.static StringgetExtension(File aFile) Get the extension of the passed file.static StringgetExtension(String sFilename) Get the extension of the passed filename.static intgetIndexOfExtension(String sFilename) Returns the index of the last extension separator character, which is a dot.static intgetIndexOfLastSeparator(String sFilename) Returns the index of the last directory separator character.static StringGet the path of the passed file name without any eventually contained filename.static StringgetPathUsingUnixSeparator(File aFile) Ensure that the path (not the absolute path!)static StringgetPathUsingUnixSeparator(String sAbsoluteFilename) Ensure that the passed path is using the Unix style separator "/" instead of the Operating System dependent one.static StringgetPathUsingWindowsSeparator(File aFile) Ensure that the path (not the absolute path!)static StringgetPathUsingWindowsSeparator(String sAbsoluteFilename) Ensure that the passed path is using the Windows style separator "\" instead of the Operating System dependent one.static StringgetRelativeToParentDirectory(File aFile, File aParentDirectory) Tries to express the passed file path relative to the passed parent directory.static StringgetSecureFilename(String sFilename) Avoid 0 byte attack.static StringgetWithoutExtension(File aFile) Get the name of the passed file without the extension.static StringgetWithoutExtension(String sFilename) Get the passed filename without the extension.static StringgetWithoutPath(File aFile) Get the name of the passed file without any eventually leading path.static StringgetWithoutPath(String sAbsoluteFilename) Get the name of the passed file without any eventually leading path.static booleanhasExtension(File aFile, String... aExtensions) Check if the passed file has one of the passed extensions.static booleanhasExtension(String sFilename, String... aExtensions) Check if the passed filename has one of the passed extensions.static booleanisEqualIgnoreFileSeparator(String sAbsoluteFilename1, String sAbsoluteFilename2) Check whether the two passed file names are equal, independent of the used separators (/ or \).static booleanisHiddenFilename(File aFile) Check if the passed filename is a Unix hidden filename.static booleanisHiddenFilename(String sFilename) Check if the passed filename is a Unix hidden filename.static booleanisPathSeparatorChar(char c) Check if the passed character is a path separation character.static booleanisSecureFilenameCharacter(char c) Check if the passed character is secure to be used in filenames.static booleanisSystemInternalDirectory(File aFile) Check if the passed file is a system directory.static booleanCheck if the passed string is a system directory.static booleanisSystemInternalDirectory(Path aFile) Check if the passed file is a system directory.static booleanCheck if the passed file is an UNC path.static booleanCheck if the passed file is an UNC path.static booleanisValidFilename(String sFilename) Check if the passed file name is valid.static booleanisValidFilenameWithPaths(String sFilename) Check if the passed filename path is valid.static booleanisWindowsLocalUNCPath(File aFile) Check if the passed file is a Windows local UNC path.static booleanisWindowsLocalUNCPath(String sFilename) Check if the passed file is a Windows local UNC path.static booleanCheck if the passed character sequence starts with a path separation character.
-
Field Details
-
EXTENSION_SEPARATOR
public static final char EXTENSION_SEPARATORThe file extension separation character.- See Also:
-
ILLEGAL_FILENAME_CHAR_REPLACEMENT
public static final char ILLEGAL_FILENAME_CHAR_REPLACEMENTThe replacement character used for illegal file name characters.- See Also:
-
PATH_CURRENT
Special name of the current path- See Also:
-
PATH_PARENT
Special name of the parent path- See Also:
-
UNIX_SEPARATOR
public static final char UNIX_SEPARATORThe Unix path separator character.- See Also:
-
UNIX_SEPARATOR_STR
The Unix path separator string. -
WINDOWS_SEPARATOR
public static final char WINDOWS_SEPARATORThe Windows separator character.- See Also:
-
WINDOWS_SEPARATOR_STR
The Windows separator string. -
UNIX_UNC_PREFIX
The prefix to identify UNC paths on Unix based systems- See Also:
-
WINDOWS_UNC_PREFIX
The prefix to identify UNC paths on Windows based systems- See Also:
-
WINDOWS_UNC_PREFIX_LOCAL1
The prefix to identify local UNC paths on Windows based systems.
Those no longer work in files in certain Java versions. See https://bugs.openjdk.org/browse/JDK-8285445- See Also:
-
WINDOWS_UNC_PREFIX_LOCAL2
The prefix to identify local UNC paths on Windows based systems
Those no longer work in files in certain Java versions. See https://bugs.openjdk.org/browse/JDK-8285445- See Also:
-
HIDDEN_FILE_PREFIX
public static final char HIDDEN_FILE_PREFIXThe prefix used for Unix hidden files- See Also:
-
-
Method Details
-
getIndexOfExtension
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
getIndexOfLastSeparator(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:
sFilename- The filename to find the last path separator in. May benull.- Returns:
- the index of the last separator character, or
CGlobal.ILLEGAL_UINTif there is no such character or the input parameter isnull. - See Also:
-
getWithoutExtension
Get the name of the passed file without the extension. If the file name contains a leading absolute path, the path is returned as well.- Parameters:
aFile- The file to extract the extension from. May benull.- Returns:
- An empty string if no extension was found, the extension without
the leading dot otherwise. If the input file is
nullthe return value isnull. - See Also:
-
getWithoutExtension
Get the passed filename without the extension. If the file name contains a leading absolute path, the path is returned as well.- Parameters:
sFilename- The filename to extract the extension from. May benullor empty.- Returns:
- An empty string if no extension was found, the extension without
the leading dot otherwise. If the input string is
nullthe return value isnull. - See Also:
-
getExtension
Get the extension of the passed file.- Parameters:
aFile- The file to extract the extension from. May benull.- Returns:
- An empty string if no extension was found, the extension without
the leading dot otherwise. Never
null. - See Also:
-
getExtension
Get the extension of the passed filename.- Parameters:
sFilename- The filename to extract the extension from. May benullor empty.- Returns:
- An empty string if no extension was found, the extension without
the leading dot otherwise. Never
null. - See Also:
-
hasExtension
Check if the passed file has one of the passed extensions. The comparison is done case insensitive even on Unix machines.- Parameters:
aFile- The file to check the extension from. May benullor empty.aExtensions- An array of extensions (without the leading dot) which are matched case insensitive. May not benull.- Returns:
trueif the file has one of the passed extensions, elsefalse.- See Also:
-
hasExtension
Check if the passed filename has one of the passed extensions. The comparison is done case insensitive even on Unix machines.- Parameters:
sFilename- The filename to check the extension from. May benullor empty.aExtensions- An array of extensions (without the leading dot) which are matched case insensitive. May not benull.- Returns:
trueif the filename has one of the passed extensions, elsefalse.- See Also:
-
getIndexOfLastSeparator
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:
sFilename- The filename to find the last path separator in,nullreturnsCGlobal.ILLEGAL_UINT.- Returns:
- The index of the last separator character, or
CGlobal.ILLEGAL_UINTif there is no such character
-
getWithoutPath
Get the name of the passed file without any eventually leading path. Note: if the passed file is a directory, the name of the directory is returned.- Parameters:
aFile- The file. May benull.- Returns:
- The name only or
nullif the passed parameter isnull.
-
getWithoutPath
Get the name of the passed file without any eventually leading path.- Parameters:
sAbsoluteFilename- The fully qualified file name. May benull.- Returns:
- The name only or
nullif the passed parameter isnull. - See Also:
-
getPath
Get the path of the passed file name without any eventually contained filename.- Parameters:
sAbsoluteFilename- The fully qualified file name. May benull.- Returns:
- The path only including the last trailing path separator character.
Returns
nullif the passed parameter isnull. - See Also:
-
getBaseName
Get the passed filename without path and without extension.
Example:/dir1/dir2/file.txtbecomesfile- Parameters:
aFile- The file to get the base name from. May benull.- Returns:
- The base name of the passed parameter. May be
nullif the parameter wasnull. - See Also:
-
getBaseName
Get the passed filename without path and without extension.
Example:/dir1/dir2/file.txtbecomesfile- Parameters:
sAbsoluteFilename- The filename to get the base name from. May benull.- Returns:
- The base name of the passed parameter. May be
nullif the parameter wasnull. - See Also:
-
getPathUsingUnixSeparator
Ensure that the path (not the absolute path!) of the passed file is using the Unix style separator "/" instead of the Operating System dependent one.- Parameters:
aFile- The file to use. May benull- Returns:
nullif the passed file isnull.- See Also:
-
getPathUsingUnixSeparator
Ensure that the passed path is using the Unix style separator "/" instead of the Operating System dependent one.- Parameters:
sAbsoluteFilename- The file name to use. May benull- Returns:
nullif the passed path isnull.- See Also:
-
getPathUsingWindowsSeparator
Ensure that the path (not the absolute path!) of the passed file is using the Windows style separator "\" instead of the Operating System dependent one.- Parameters:
aFile- The file to use. May benull- Returns:
nullif the passed file isnull.- See Also:
-
getPathUsingWindowsSeparator
Ensure that the passed path is using the Windows style separator "\" instead of the Operating System dependent one.- Parameters:
sAbsoluteFilename- The file name to use. May benull- Returns:
nullif the passed path isnull.- See Also:
-
isEqualIgnoreFileSeparator
public static boolean isEqualIgnoreFileSeparator(@Nullable String sAbsoluteFilename1, @Nullable String sAbsoluteFilename2) Check whether the two passed file names are equal, independent of the used separators (/ or \).- Parameters:
sAbsoluteFilename1- First file name. May benull.sAbsoluteFilename2- Second file name. May benull.- Returns:
trueif they are equal,falseotherwise.- See Also:
-
getSecureFilename
Avoid 0 byte attack. E.g. file name "test.java .txt" is internally represented as "test.java" but ends with ".txt".
Note: the passed file name is NOT decoded (e.g. %20 stays %20 and will not be converted to a space).- Parameters:
sFilename- The file name to check. May benull.- Returns:
nullif the input string isnullor everything up to the 0-byte.
-
isValidFilename
Check if the passed file name is valid. It checks for illegal prefixes that affects compatibility to Windows, illegal characters within a filename and forbidden suffixes. This method fits only for filenames on one level. If you want to check a full path, useisValidFilenameWithPaths(String).- Parameters:
sFilename- The filename to check. May benull.- Returns:
falseif the passed filename isnullor empty or invalid.trueif the filename is not empty and valid.- See Also:
-
isValidFilenameWithPaths
Check if the passed filename path is valid. In contrast toisValidFilename(String)this method can also handle filenames including paths.- Parameters:
sFilename- The filename to be checked for validity.- Returns:
trueif all path elements of the filename are valid,falseif at least one element is invalid- See Also:
-
getAsSecureValidFilename
Convert the passed filename into a valid filename by performing the following actions:- Remove everything after a potential \0 character
- Remove all characters that are invalid at the end of a file name
- Replace all characters that are invalid inside a filename with a underscore
- If the filename is invalid on Windows platforms it is prefixed with an underscore.
- Parameters:
sFilename- The filename to be made value. May benull.- Returns:
nullif the input filename wasnullor if it consisted only of characters invalid for a filename; the potentially modified filename otherwise but never an empy string.- See Also:
-
isSecureFilenameCharacter
public static boolean isSecureFilenameCharacter(char c) Check if the passed character is secure to be used in filenames. Therefore it must be ≥ 0x20 and < 0x80.- Parameters:
c- The character to check- Returns:
trueif it is valid,falseif not
-
getAsSecureValidASCIIFilename
Replace all non-ASCII characters from the filename (e.g. German Umlauts) with underscores. Before replacing non-ASCII characters the filename is made valid usinggetAsSecureValidFilename(String).- Parameters:
sFilename- Input file name. May not benull.- Returns:
nullif the input filename wasnull. The file name containing only ASCII characters. The returned value is never an empty String.- See Also:
-
getAsSecureValidASCIIFilename
@Nullable @Nonempty public static String getAsSecureValidASCIIFilename(@Nullable String sFilename, char cReplacementChar) Replace all non-ASCII characters from the filename (e.g. German Umlauts) with a replacement char. Before replacing non-ASCII characters the filename is made valid usinggetAsSecureValidFilename(String).- Parameters:
sFilename- Input file name. May not benull.cReplacementChar- The replacement character to be used for insecure filenames.- Returns:
nullif the input filename wasnull. The file name containing only ASCII characters. The returned value is never an empty String.- See Also:
-
isPathSeparatorChar
public static boolean isPathSeparatorChar(char c) Check if the passed character is a path separation character. This method handles both Windows- and Unix-style path separation characters.- Parameters:
c- The character to check.- Returns:
trueif the character is a path separation character,falseotherwise.
-
startsWithPathSeparatorChar
Check if the passed character sequence starts with a path separation character.- Parameters:
s- The character sequence to check. May benullor empty.- Returns:
trueif the character sequences starts with a Windows- or Unix-style path character.- See Also:
-
endsWithPathSeparatorChar
Check if the passed character sequence ends with a path separation character.- Parameters:
s- The character sequence to check. May benullor empty.- Returns:
trueif the character sequences ends with a Windows- or Unix-style path character.- See Also:
-
containsPathSeparatorChar
Check if the passed String contains at least one path separator char (either Windows or Unix style).- Parameters:
s- The string to check. May benull.- Returns:
trueif the passed string is notnulland contains at least one separator.
-
isSystemInternalDirectory
- Parameters:
aFile- The file to be checked. May benull.- Returns:
trueif the passed file name (not the path) matches any of the special directory names,falseof the passed file isnullor does not denote a special directory.- See Also:
-
isSystemInternalDirectory
- Parameters:
aFile- The file to be checked. May benull.- Returns:
trueif the passed file name (not the path) matches any of the special directory names,falseof the passed file isnullor does not denote a special directory.- See Also:
-
isSystemInternalDirectory
- Parameters:
s- The value to be checked. May benull.- Returns:
trueif the passed string matches any of the special directory names,falseof the passed string isnullor does not denote a special directory.- See Also:
-
isUNCPath
Check if the passed file is an UNC path. UNC paths are identified by starting with "//" or "\\".- Parameters:
aFile- The file to be checked. May not benull.- Returns:
trueif the file points to an UNC path,falseif not.- See Also:
-
isUNCPath
Check if the passed file is an UNC path. UNC paths are identified by starting with "//" or "\\".- Parameters:
sFilename- The absolute filename to be checked. May not benull.- Returns:
trueif the file points to an UNC path,falseif not.- See Also:
-
isWindowsLocalUNCPath
Check if the passed file is a Windows local UNC path. This type is identified by starting with "\\?\" or "\\.\".- Parameters:
aFile- The file to be checked. May not benull.- Returns:
trueif the file points to an UNC path,falseif not.- See Also:
-
isWindowsLocalUNCPath
Check if the passed file is a Windows local UNC path. This type is identified by starting with "\\?\" or "\\.\".- Parameters:
sFilename- The absolute filename to be checked. May not benull.- Returns:
trueif the file points to a Windows local UNC path,falseif not.- See Also:
-
getCleanPath
Get a clean path of the passed file resolving all "." and ".." paths.
Note: in caseFileHelper.getCanonicalPath(File)fails,getCleanPath(String)is used as a fallback.
Note 2: no cleansing operations beside "." and ".." are returned. You need to ensure yourself, that the returned file name is valid!- Parameters:
aFile- The file to be cleaned. May not benull.- Returns:
- The cleaned path and never
null. - See Also:
-
getCleanPath
Clean the path by removing all ".." and "." path elements.- Parameters:
sPath- The path to be cleaned.- Returns:
- The cleaned path or
nullif the input parameter wasnull. - See Also:
-
getCleanConcatenatedUrlPath
@Nonnull public static String getCleanConcatenatedUrlPath(@Nonnull String sURL, @Nonnull String sPath) Concatenate a base URL and a sub path incl. the path cleansing. More or less the same as callinggetCleanPath (sURL + "/" + sPath)- Parameters:
sURL- The base URL. May not benull.sPath- The path to append. May not benull.- Returns:
- The combined, cleaned path.
- See Also:
-
ensurePathStartingWithSeparator
@Nullable @CheckReturnValue public static String ensurePathStartingWithSeparator(@Nullable String sPath) Ensure that the passed path starts with a directory separator character. If the passed path starts with either '\\' or '/' no changes are performed.- Parameters:
sPath- The path to be checked.- Returns:
- The path that is ensured to start with the directory separator of the current operating system.
- See Also:
-
ensurePathEndingWithoutSeparator
@Nullable @CheckReturnValue public static String ensurePathEndingWithoutSeparator(@Nullable String sPath) Ensure that the passed path does NOT end with a directory separator character. Any number of trailing '\\' or '/' are removed.- Parameters:
sPath- The path to be checked.- Returns:
- The path that is ensured to NOT end with the directory separator.
- See Also:
-
ensurePathEndingWithSeparator
@Nullable @CheckReturnValue public static String ensurePathEndingWithSeparator(@Nullable String sPath) Ensure that the passed path ends with a directory separator character. If the passed path ends with either '\\' or '/' no changes are performed.- Parameters:
sPath- The path to be checked.- Returns:
- The path that is ensured to end with the directory separator of the current operating system.
- See Also:
-
getRelativeToParentDirectory
@Nullable public static String getRelativeToParentDirectory(@Nonnull File aFile, @Nullable File aParentDirectory) Tries to express the passed file path relative to the passed parent directory. If the parent directory is null or not actually a parent of the passed file, the passed file name will be returned unchanged.- Parameters:
aFile- The file which is to be described relatively. May not benull.aParentDirectory- The parent directory of the file to which the relative path expression will relate to. May benull.- Returns:
- The relative path or the unchanged absolute file path using Unix
path separators instead of Operating System dependent separator. Or
nullif the passed file contains a path traversal at the beginning - See Also:
-
getAbsoluteWithEnsuredParentDirectory
@Nullable public static String getAbsoluteWithEnsuredParentDirectory(@Nonnull File aParentDirectory, @Nonnull String sFilePath) Get a concatenated absolute path consisting of the parent directory and the file path. It is ensured that the resulting (cleaned) filename is still the same or a child of the passed parent directory. If the file path contains some directory traversal elements (e.g. starting with "..")nullis returned.- Parameters:
aParentDirectory- The parent directory to be ensured. May not benull.sFilePath- The file path to be appended to the passed parent directory. May not benull.- Returns:
nullif the parent directory would be changed with the passed file path - the concatenated cleaned path otherwise (using Unix separators).- See Also:
-
isHiddenFilename
Check if the passed filename is a Unix hidden filename.- Parameters:
aFile- The file to check. May benull.- Returns:
trueif the file is notnulland the name starts with a dot.- See Also:
-
isHiddenFilename
Check if the passed filename is a Unix hidden filename.- Parameters:
sFilename- The filename to check. May benull.- Returns:
trueif the filename is neithernullnor empty and starts with a dot.- See Also:
-