Class FilenameUtils

java.lang.Object
org.apache.commons.io.FilenameUtils

public class FilenameUtils extends Object
General file name and file path manipulation utilities.

When dealing with file names 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 file name (example C:\dev\project\file.txt):

  • the prefix - C:\
  • the path - dev\project\
  • the full path - C:\dev\project\
  • the name - file.txt
  • the base name - file
  • the extension - txt
Note that this class works best if directory file names end with a separator. If you omit the last separator, it is impossible to determine if the file name 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.

Since:
1.1
  • Field Details

    • EMPTY_STRING_ARRAY

      private static final String[] EMPTY_STRING_ARRAY
    • EMPTY_STRING

      private static final String EMPTY_STRING
      See Also:
    • NOT_FOUND

      private static final int NOT_FOUND
      See Also:
    • EXTENSION_SEPARATOR

      public static final char EXTENSION_SEPARATOR
      The extension separator character.
      Since:
      1.4
      See Also:
    • EXTENSION_SEPARATOR_STR

      public static final String EXTENSION_SEPARATOR_STR
      The extension separator String.
      Since:
      1.4
    • UNIX_SEPARATOR

      private static final char UNIX_SEPARATOR
      The Unix separator character.
      See Also:
    • WINDOWS_SEPARATOR

      private static final char WINDOWS_SEPARATOR
      The Windows separator character.
      See Also:
    • SYSTEM_SEPARATOR

      private static final char SYSTEM_SEPARATOR
      The system separator character.
    • OTHER_SEPARATOR

      private static final char OTHER_SEPARATOR
      The separator character that is the opposite of the system separator.
    • IPV4_PATTERN

      private static final Pattern IPV4_PATTERN
    • IPV4_MAX_OCTET_VALUE

      private static final int IPV4_MAX_OCTET_VALUE
      See Also:
    • IPV6_MAX_HEX_GROUPS

      private static final int IPV6_MAX_HEX_GROUPS
      See Also:
    • IPV6_MAX_HEX_DIGITS_PER_GROUP

      private static final int IPV6_MAX_HEX_DIGITS_PER_GROUP
      See Also:
    • MAX_UNSIGNED_SHORT

      private static final int MAX_UNSIGNED_SHORT
      See Also:
    • BASE_16

      private static final int BASE_16
      See Also:
    • REG_NAME_PART_PATTERN

      private static final Pattern REG_NAME_PART_PATTERN
  • Constructor Details

    • FilenameUtils

      public FilenameUtils()
      Instances should NOT be constructed in standard programming.
  • Method Details

    • isSystemWindows

      static boolean isSystemWindows()
      Determines if Windows file system is in use.
      Returns:
      true if the system is Windows
    • isSeparator

      private static boolean isSeparator(char ch)
      Checks if the character is a separator.
      Parameters:
      ch - the character to check
      Returns:
      true if it is a separator character
    • normalize

      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 - the fileName to normalize, null returns null
      Returns:
      the normalized fileName, or null if invalid. Null bytes inside string will be removed
    • normalize

      public static String normalize(String fileName, boolean unixSeparator)
      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 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             -->   null
       
      The output will be the same on both Unix and Windows including the separator character.
      Parameters:
      fileName - the fileName to normalize, null returns null
      unixSeparator - true if a unix separator should be used or false if a windows separator should be used.
      Returns:
      the normalized fileName, or null if invalid. Null bytes inside string will be removed
      Since:
      2.0
    • normalizeNoEndSeparator

      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 - the fileName to normalize, null returns null
      Returns:
      the normalized fileName, or null if invalid. Null bytes inside string will be removed
    • normalizeNoEndSeparator

      public static String normalizeNoEndSeparator(String fileName, boolean unixSeparator)
      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 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
       
      Parameters:
      fileName - the fileName to normalize, null returns null
      unixSeparator - true if a unix separator should be used or false if a windows separator should be used.
      Returns:
      the normalized fileName, or null if invalid. Null bytes inside string will be removed
      Since:
      2.0
    • doNormalize

      private static String doNormalize(String fileName, char separator, boolean keepSeparator)
      Internal method to perform the normalization.
      Parameters:
      fileName - the fileName
      separator - The separator character to use
      keepSeparator - true to keep the final separator
      Returns:
      the normalized fileName. Null bytes inside string will be removed.
    • concat

      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 - the base path to attach to, always treated as a path
      fullFileNameToAdd - the fileName (or path) to attach to the base
      Returns:
      the concatenated path, or null if invalid. Null bytes inside string will be removed
    • directoryContains

      public static boolean directoryContains(String canonicalParent, String canonicalChild) throws IOException
      Determines whether the parent directory contains the child element (a file or directory).

      The files names are expected to be normalized.

      Edge cases:
      • A directory must not be null: if null, throw IllegalArgumentException
      • A directory does not contain itself: return false
      • A null child file is not contained in any parent: return false
      Parameters:
      canonicalParent - the file to consider as the parent.
      canonicalChild - the file to consider as the child.
      Returns:
      true is the candidate leaf is under by the specified composite. False otherwise.
      Throws:
      IOException - if an IO error occurs while checking the files.
      Since:
      2.2
      See Also:
    • separatorsToUnix

      public static String separatorsToUnix(String path)
      Converts all separators to the Unix separator of forward slash.
      Parameters:
      path - the path to be changed, null ignored
      Returns:
      the updated path
    • separatorsToWindows

      public static String separatorsToWindows(String path)
      Converts all separators to the Windows separator of backslash.
      Parameters:
      path - the path to be changed, null ignored
      Returns:
      the updated path
    • separatorsToSystem

      public static String separatorsToSystem(String path)
      Converts all separators to the system separator.
      Parameters:
      path - the path to be changed, null ignored
      Returns:
      the updated path
    • getPrefixLength

      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
       \\\a\b\c.txt        -->  error, length = -1
      
       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)
       //server/a/b/c.txt  --> "//server/"
       ///a/b/c.txt        --> error, length = -1
       

      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. Note that a leading // (or \\) is used to indicate a UNC name on Windows. These must be followed by a server name, so double-slashes are not collapsed to a single slash at the start of the fileName.

      Parameters:
      fileName - the fileName to find the prefix in, null returns -1
      Returns:
      the length of the prefix, -1 if invalid or null
    • indexOfLastSeparator

      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 - 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

      public static int indexOfExtension(String fileName) throws IllegalArgumentException
      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, with the exception of a possible IllegalArgumentException on Windows (see below).

      Note: This method used to have a hidden problem for names like "foo.exe:bar.txt". In this case, the name wouldn't be the name of a file, but the identifier of an alternate data stream (bar.txt) on the file foo.exe. The method used to return ".txt" here, which would be misleading. Commons IO 2.7, and later versions, are throwing an IllegalArgumentException for names like this.
      Parameters:
      fileName - the fileName to find the last extension separator in, null returns -1
      Returns:
      the index of the last extension separator character, or -1 if there is no such character
      Throws:
      IllegalArgumentException - Windows only: The fileName parameter is, in fact, the identifier of an Alternate Data Stream, for example "foo.exe:bar.txt".
    • getPrefix

      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 - the fileName to query, null returns null
      Returns:
      the prefix of the file, null if invalid. Null bytes inside string will be removed
    • getPath

      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 - the fileName to query, null returns null
      Returns:
      the path of the file, an empty string if none exists, null if invalid. Null bytes inside string will be removed
    • getPathNoEndSeparator

      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 - the fileName to query, null returns null
      Returns:
      the path of the file, an empty string if none exists, null if invalid. Null bytes inside string will be removed
    • doGetPath

      private static String doGetPath(String fileName, int separatorAdd)
      Does the work of getting the path.
      Parameters:
      fileName - the fileName
      separatorAdd - 0 to omit the end separator, 1 to return it
      Returns:
      the path. Null bytes inside string will be removed
    • getFullPath

      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 - the fileName to query, null returns null
      Returns:
      the path of the file, an empty string if none exists, null if invalid
    • getFullPathNoEndSeparator

      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 - the fileName to query, null returns null
      Returns:
      the path of the file, an empty string if none exists, null if invalid
    • doGetFullPath

      private static String doGetFullPath(String fileName, boolean includeSeparator)
      Does the work of getting the path.
      Parameters:
      fileName - the fileName
      includeSeparator - true to include the end separator
      Returns:
      the path
    • getName

      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 - the fileName to query, null returns null
      Returns:
      the name of the file without the path, or an empty string if none exists. Null bytes inside string will be removed
    • failIfNullBytePresent

      private static void failIfNullBytePresent(String path)
      Check the input for null bytes, a sign of unsanitized data being passed to to file level functions. This may be used for poison byte attacks.
      Parameters:
      path - the path to check
    • getBaseName

      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 - the fileName to query, null returns null
      Returns:
      the name of the file without the path, or an empty string if none exists. Null bytes inside string will be removed
    • getExtension

      public static String getExtension(String fileName) throws IllegalArgumentException
      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, with the exception of a possible IllegalArgumentException on Windows (see below).

      Note: This method used to have a hidden problem for names like "foo.exe:bar.txt". In this case, the name wouldn't be the name of a file, but the identifier of an alternate data stream (bar.txt) on the file foo.exe. The method used to return ".txt" here, which would be misleading. Commons IO 2.7, and later versions, are throwing an IllegalArgumentException for names like this.

      Parameters:
      fileName - the fileName to retrieve the extension of.
      Returns:
      the extension of the file or an empty string if none exists or null if the fileName is null.
      Throws:
      IllegalArgumentException - Windows only: The fileName parameter is, in fact, the identifier of an Alternate Data Stream, for example "foo.exe:bar.txt".
    • getAdsCriticalOffset

      private static int getAdsCriticalOffset(String fileName)
      Special handling for NTFS ADS: Don't accept colon in the fileName.
      Parameters:
      fileName - a file name
      Returns:
      ADS offsets.
    • removeExtension

      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 - the fileName to query, null returns null
      Returns:
      the fileName minus the extension
    • equals

      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 - the first fileName to query, may be null
      fileName2 - the second fileName to query, may be null
      Returns:
      true if the fileNames are equal, null equals null
      See Also:
    • equalsOnSystem

      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 - the first fileName to query, may be null
      fileName2 - the second fileName to query, may be null
      Returns:
      true if the fileNames are equal, null equals null
      See Also:
    • equalsNormalized

      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 - the first fileName to query, may be null
      fileName2 - the second fileName to query, may be null
      Returns:
      true if the fileNames are equal, null equals null
      See Also:
    • equalsNormalizedOnSystem

      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 - the first fileName to query, may be null
      fileName2 - the second fileName to query, may be null
      Returns:
      true if the fileNames are equal, null equals null
      See Also:
    • equals

      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 - the first fileName to query, may be null
      fileName2 - the second fileName to query, may be null
      normalized - whether to normalize the fileNames
      caseSensitivity - what case sensitivity rule to use, null means case-sensitive
      Returns:
      true if the fileNames are equal, null equals null
      Since:
      1.3
    • isExtension

      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 - the fileName to query, null returns false
      extension - the extension to check for, null or empty checks for no extension
      Returns:
      true if the fileName has the specified extension
      Throws:
      IllegalArgumentException - if the supplied fileName contains null bytes
    • isExtension

      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 - the fileName to query, null returns false
      extensions - the extensions to check for, null checks for no extension
      Returns:
      true if the fileName is one of the extensions
      Throws:
      IllegalArgumentException - if the supplied fileName contains null bytes
    • isExtension

      public static boolean isExtension(String fileName, Collection<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 - the fileName to query, null returns false
      extensions - the extensions to check for, null checks for no extension
      Returns:
      true if the fileName is one of the extensions
      Throws:
      IllegalArgumentException - if the supplied fileName contains null bytes
    • wildcardMatch

      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 (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", "*.????")     --> false
       
      N.B. the sequence "*?" does not work properly at present in match strings.
      Parameters:
      fileName - the fileName to match on
      wildcardMatcher - the wildcard string to match against
      Returns:
      true if the fileName matches the wildcard string
      See Also:
    • wildcardMatchOnSystem

      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 (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", "*.????")     --> false
       
      N.B. the sequence "*?" does not work properly at present in match strings.
      Parameters:
      fileName - the fileName to match on
      wildcardMatcher - the wildcard string to match against
      Returns:
      true if the fileName matches the wildcard string
      See Also:
    • wildcardMatch

      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 (zero or more) wildcard characters. N.B. the sequence "*?" does not work properly at present in match strings.

      Parameters:
      fileName - the fileName to match on
      wildcardMatcher - the wildcard string to match against
      caseSensitivity - what case sensitivity rule to use, null means case-sensitive
      Returns:
      true if the fileName matches the wildcard string
      Since:
      1.3
    • splitOnTokens

      static String[] splitOnTokens(String text)
      Splits a string into a number of tokens. The text is split by '?' and '*'. Where multiple '*' occur consecutively they are collapsed into a single '*'.
      Parameters:
      text - the text to split
      Returns:
      the array of tokens, never null
    • isValidHostName

      private static boolean isValidHostName(String name)
      Checks whether a given string is a valid host name according to RFC 3986.

      Accepted are IP addresses (v4 and v6) as well as what the RFC calls a "reg-name". Percent encoded names don't seem to be valid names in UNC paths.

      Parameters:
      name - the hostname to validate
      Returns:
      true if the given name is a valid host name
      See Also:
      • "https://tools.ietf.org/html/rfc3986#section-3.2.2"
    • isIPv4Address

      private static boolean isIPv4Address(String name)
      Checks whether a given string represents a valid IPv4 address.
      Parameters:
      name - the name to validate
      Returns:
      true if the given name is a valid IPv4 address
    • isIPv6Address

      private static boolean isIPv6Address(String inet6Address)
      Checks whether a given string represents a valid IPv6 address.
      Parameters:
      inet6Address - the name to validate
      Returns:
      true if the given name is a valid IPv6 address
    • isRFC3986HostName

      private static boolean isRFC3986HostName(String name)
      Checks whether a given string is a valid host name according to RFC 3986 - not accepting IP addresses.
      Parameters:
      name - the hostname to validate
      Returns:
      true if the given name is a valid host name
      See Also:
      • "https://tools.ietf.org/html/rfc3986#section-3.2.2"