Class FilenameUtils


  • public class FilenameUtils
    extends java.lang.Object
    Utility methods for General filename and filepath manipulation.

    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
    • Constructor Summary

      Constructors 
      Constructor Description
      FilenameUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.io.File generateSafetyUniqueFile​(java.io.File file)
      Creates and returns a safe file name on the system without duplication in the specified directory.
      static java.lang.String getBaseName​(java.lang.String filename)
      Gets the base name, minus the full path and extension, from a full filename.
      static java.lang.String getExtension​(java.lang.String filename)
      Gets the extension of a filename.
      static java.lang.String getFullPath​(java.lang.String filename)
      Gets the path from a full filename.
      static java.lang.String getFullPathWithEndSeparator​(java.lang.String filename)
      Gets the path with end separator from a full filename.
      static java.lang.String getName​(java.lang.String filename)
      Gets the name minus the path from a full filename.
      static java.io.File getUniqueFile​(java.io.File srcFile)
      Returns a file name that does not overlap in the specified directory.
      static java.io.File getUniqueFile​(java.io.File srcFile, char extSeparator)
      Returns a file name that does not overlap in the specified directory.
      static int indexOfExtension​(java.lang.String filename)
      Returns the index of the last extension separator character, which is a dot.
      static int indexOfLastSeparator​(java.lang.String filename)
      Returns the index of the last directory separator character.
      static boolean isValidFileExtension​(java.lang.String filename, java.lang.String allowedFileExtensions, java.lang.String deniedFileExtensions)
      Checks whether the extension of the filename is valid.
      static java.lang.String removeExtension​(java.lang.String filename)
      Removes the extension from a filename.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • FilenameUtils

        public FilenameUtils()
    • Method Detail

      • getName

        public static java.lang.String getName​(java.lang.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
      • getBaseName

        public static java.lang.String getBaseName​(java.lang.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
      • getExtension

        public static java.lang.String getExtension​(java.lang.String filename)
        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

        public static java.lang.String removeExtension​(java.lang.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
      • indexOfLastSeparator

        public static int indexOfLastSeparator​(java.lang.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​(java.lang.String filename)
        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
      • getFullPath

        public static java.lang.String getFullPath​(java.lang.String filename)
        Gets the path from a full filename.
        Parameters:
        filename - a full filename
        Returns:
        the path
      • getFullPathWithEndSeparator

        public static java.lang.String getFullPathWithEndSeparator​(java.lang.String filename)
        Gets the path with end separator from a full filename.
        Parameters:
        filename - a full filename
        Returns:
        the path
      • isValidFileExtension

        public static boolean isValidFileExtension​(java.lang.String filename,
                                                   java.lang.String allowedFileExtensions,
                                                   java.lang.String deniedFileExtensions)
        Checks whether the extension of the filename is valid. The extension check is case-sensitive on all platforms.
        Parameters:
        filename - the filename to query, null returns false
        allowedFileExtensions - the allowed file extensions
        deniedFileExtensions - the denied file extensions
        Returns:
        true if is valid file extension; false otherwise
      • getUniqueFile

        public static java.io.File getUniqueFile​(java.io.File srcFile)
                                          throws java.io.IOException
        Returns a file name that does not overlap in the specified directory. If a duplicate file name exists, it is returned by appending a number after the file name.
        Parameters:
        srcFile - the file to seek
        Returns:
        an unique file
        Throws:
        java.io.IOException - if failed to obtain an unique file
      • getUniqueFile

        public static java.io.File getUniqueFile​(java.io.File srcFile,
                                                 char extSeparator)
                                          throws java.io.IOException
        Returns a file name that does not overlap in the specified directory. If a duplicate file name exists, it is returned by appending a number after the file name.
        Parameters:
        srcFile - the file to seek
        extSeparator - the extension separator
        Returns:
        an unique file
        Throws:
        java.io.IOException - if failed to obtain an unique file
      • generateSafetyUniqueFile

        public static java.io.File generateSafetyUniqueFile​(java.io.File file)
                                                     throws java.io.IOException
        Creates and returns a safe file name on the system without duplication in the specified directory. If a duplicate file name exists, it is returned by appending a number after the file name. File extensions are separated by '_' character.
         ex) 1111111111_txt
         
        Parameters:
        file - the file to seek
        Returns:
        an unique file
        Throws:
        java.io.IOException - if failed to obtain an unique file