public class FilenameUtils
extends java.lang.Object
This class defines six components within a filename (example C:\dev\project\file.txt):
Constructor | Description |
---|---|
FilenameUtils() |
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.
|
public static java.lang.String getName(java.lang.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 java.lang.String getBaseName(java.lang.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 java.lang.String getExtension(java.lang.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 java.lang.String removeExtension(java.lang.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(java.lang.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(java.lang.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 java.lang.String getFullPath(java.lang.String filename)
filename
- a full filenamepublic static java.lang.String getFullPathWithEndSeparator(java.lang.String filename)
filename
- a full filenamepublic static boolean isValidFileExtension(java.lang.String filename, java.lang.String allowedFileExtensions, java.lang.String deniedFileExtensions)
filename
- the filename to query, null returns falseallowedFileExtensions
- the allowed file extensionsdeniedFileExtensions
- the denied file extensionspublic static java.io.File getUniqueFile(java.io.File srcFile) throws java.io.IOException
srcFile
- the file to seekjava.io.IOException
- if failed to obtain an unique filepublic static java.io.File getUniqueFile(java.io.File srcFile, char extSeparator) throws java.io.IOException
srcFile
- the file to seekextSeparator
- the extension separatorjava.io.IOException
- if failed to obtain an unique filepublic static java.io.File generateSafetyUniqueFile(java.io.File file) throws java.io.IOException
ex) 1111111111_txt
file
- the file to seekjava.io.IOException
- if failed to obtain an unique fileCopyright © 2008–2018 The Aspectran Project. All rights reserved.