Class FilenameUtils
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 -
Method Summary
Modifier and TypeMethodDescriptionstatic File
generateSafetyUniqueFile
(File file) Creates and returns a system-safe file name without duplicates in the specified directory.static File
generateSafetyUniqueFile
(File file, String extSeparator) Creates and returns a system-safe file name without duplicates in the specified directory.static File
generateUniqueFile
(File srcFile) Returns a file name that does not overlap in the specified directory.static File
generateUniqueFile
(File srcFile, String extSeparator) Returns a file name that does not overlap in the specified directory.static String
getBaseName
(String filename) Gets the base name, minus the full path and extension, from a full filename.static String
getExtension
(String filename) Extract the file extension from the given filename.static String
getFullPath
(String filename) Gets the path from a full filename.static String
getFullPathWithEndSeparator
(String filename) Gets the path with end separator from a full filename.static String
Gets the name minus the path from a full filename.static int
indexOfExtension
(String filename) Returns the index of the last extension separator character, which is a dot.static int
indexOfLastSeparator
(String filename) Returns the index of the last directory separator character.static boolean
isValidFileExtension
(String filename, String allowedFileExtensions, String deniedFileExtensions) Checks whether the extension of the filename is valid.static String
recoverExtension
(String uniqueFilename) Recovers the extension from a unique file name.static String
removeExtension
(String filename) Removes the extension from a filename.
-
Constructor Details
-
FilenameUtils
public FilenameUtils()
-
-
Method Details
-
getName
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
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
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
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
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
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
Gets the path from a full filename.- Parameters:
filename
- a full filename- Returns:
- the path
-
getFullPathWithEndSeparator
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 falseallowedFileExtensions
- the allowed file extensionsdeniedFileExtensions
- the denied file extensions- Returns:
- true if is valid file extension; false otherwise
-
generateUniqueFile
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
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 seekextSeparator
- the file extension separator- Returns:
- a unique file
- Throws:
IOException
- if failed to obtain a unique file
-
generateSafetyUniqueFile
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 seekextSeparator
- the file extension separator- Returns:
- a unique file
- Throws:
IOException
- if failed to obtain a unique file
-
recoverExtension
Recovers the extension from a unique file name.- Parameters:
uniqueFilename
- a unique file name- Returns:
- file name with recovered extension
-