Class FilenameUtils

java.lang.Object
com.aspectran.utils.FilenameUtils

public abstract class FilenameUtils extends 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 Details

    • FilenameUtils

      public FilenameUtils()
  • Method Details

    • getName

      @NonNull 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
    • 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
    • getExtension

      public static String getExtension(String filename)
      Extract the file extension from the given 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 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
    • 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)
      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 String getFullPath(String filename)
      Gets the path from a full filename.
      Parameters:
      filename - a full filename
      Returns:
      the path
    • getFullPathWithEndSeparator

      public static String getFullPathWithEndSeparator(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(String filename, String allowedFileExtensions, 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
    • generateUniqueFile

      public static File generateUniqueFile(File srcFile) throws 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:
      a unique file
      Throws:
      IOException - if failed to obtain a unique file
    • generateUniqueFile

      public static File generateUniqueFile(File srcFile, String extSeparator) throws 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 file extension separator
      Returns:
      a unique file
      Throws:
      IOException - if failed to obtain a unique file
    • generateSafetyUniqueFile

      public static File generateSafetyUniqueFile(File file) throws IOException
      Creates and returns a system-safe file name without duplicates in the specified directory. If there is a duplicate file name, a number is added after the file name and returned. File extensions are separated by '.' character.
       ex) 1111111111_1.txt
       
      Parameters:
      file - the file to seek
      Returns:
      a unique file
      Throws:
      IOException - if failed to obtain a unique file
    • generateSafetyUniqueFile

      public static File generateSafetyUniqueFile(@NonNull File file, String extSeparator) throws IOException
      Creates and returns a system-safe file name without duplicates in the specified directory. If there is a duplicate file name, a number is added after the file name and returned. If the file extension separator is specified as '_', the file name can be obtained in the following format.
       ex) 1111111111_txt
       
      Parameters:
      file - the file to seek
      extSeparator - the file extension separator
      Returns:
      a unique file
      Throws:
      IOException - if failed to obtain a unique file
    • recoverExtension

      @NonNull public static String recoverExtension(String uniqueFilename)
      Recovers the extension from a unique file name.
      Parameters:
      uniqueFilename - a unique file name
      Returns:
      file name with recovered extension