Class FilenameUtils

java.lang.Object
org.zeroturnaround.zip.commons.FilenameUtils

public class FilenameUtils extends Object
This is a class that has been made significantly smaller (deleted a bunch of methods) and originally is from the Apache Commons IO 2.2 package (the latest version that supports Java 1.5). All license and other documentation is intact. General filename and filepath manipulation utilities.

When dealing with filenames you can hit problems when moving from a Windows based development machine to a Unix based production machine. This class aims to help avoid those problems.

NOTE: You may be able to avoid using this class entirely simply by using JDK File objects and the two argument constructor File(File,String).

Most methods on this class are designed to work the same on both Unix and Windows. Those that don't include 'System', 'Unix' or 'Windows' in their name.

Most methods recognise both separators (forward and back), and both sets of prefixes. See the javadoc of each method for details.

This class defines six components within a filename (example C:\dev\project\file.txt):

  • 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 filenames end with a separator. If you omit the last separator, it is impossible to determine if the filename corresponds to a file or a directory. As a result, we have chosen to say it corresponds to a file.

This class only supports Unix and Windows style names. Prefixes are matched as follows:

 Windows:
 a\b\c.txt           --> ""          --> relative
 \a\b\c.txt          --> "\"         --> current drive absolute
 C:a\b\c.txt         --> "C:"        --> drive relative
 C:\a\b\c.txt        --> "C:\"       --> absolute
 \\server\a\b\c.txt  --> "\\server\" --> UNC
 
 Unix:
 a/b/c.txt           --> ""          --> relative
 /a/b/c.txt          --> "/"         --> absolute
 ~/a/b/c.txt         --> "~/"        --> current user
 ~                   --> "~/"        --> current user (slash added)
 ~user/a/b/c.txt     --> "~user/"    --> named user
 ~user               --> "~user/"    --> named user (slash added)
 
Both prefix styles are matched always, irrespective of the machine that you are currently running on.

Origin of code: Excalibur, Alexandria, Tomcat, Commons-Utils.

Since:
Commons IO 1.1
Version:
$Id: FilenameUtils.java 609870 2008-01-08 04:46:26Z niallp $
  • Field Details

    • EXTENSION_SEPARATOR

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

      public static final String EXTENSION_SEPARATOR_STR
      The extension separator String.
      Since:
      Commons IO 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.
  • 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
    • 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
       
       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