public class FilenameUtils extends Object
This class defines six components within a filename (example C:\dev\project\file.txt):
Constructor and Description |
---|
FilenameUtils() |
Modifier and Type | Method and Description |
---|---|
static File |
generateSafetyUniqueFile(File file)
Creates and returns a safe file name on the system without duplication 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)
Gets the extension of a filename.
|
static String |
getFullPath(String filename)
Gets the path from a full filename.
|
static String |
getFullPathWithEndSeprator(String filename)
Gets the path with end separator from a full filename.
|
static String |
getName(String filename)
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 |
removeExtension(String filename)
Removes the extension from a filename.
|
static File |
seekUniqueFile(File srcFile)
Returns a file name that does not overlap in the specified directory.
|
static File |
seekUniqueFile(File srcFile,
char extSeparator)
Returns a file name that does not overlap in the specified directory.
|
public static String getName(String 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.
filename
- the filename to query, null returns nullpublic static String getBaseName(String 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.
filename
- the filename to query, null returns nullpublic static String getExtension(String 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.
filename
- the filename to retrieve the extension of.public static String removeExtension(String 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.
filename
- the filename to query, null returns nullpublic static int indexOfLastSeparator(String filename)
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.
filename
- the filename to find the last path separator in, null returns -1public static int indexOfExtension(String filename)
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.
filename
- the filename to find the last path separator in, null returns -1public static String getFullPath(String filename)
filename
- a full filenamepublic static String getFullPathWithEndSeprator(String filename)
filename
- a full filenamepublic static boolean isValidFileExtension(String filename, String allowedFileExtensions, String deniedFileExtensions)
filename
- he filename to query, null returns falseallowedFileExtensions
- the allowed file extensionsdeniedFileExtensions
- the denied file extensionspublic static File seekUniqueFile(File srcFile) throws IOException
srcFile
- the file to seekIOException
- if failed to obtain an unique filepublic static File seekUniqueFile(File srcFile, char extSeparator) throws IOException
srcFile
- the file to seekextSeparator
- the extension separatorIOException
- if failed to obtain an unique filepublic static File generateSafetyUniqueFile(File file) throws IOException
ex) 1111111111_txt
file
- the file to seekIOException
- if failed to obtain an unique fileCopyright © 2008–2017 Aspectran.com. All rights reserved.