Class Filenames
- Author:
- Garret Wilson
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
Utilities for working directly with filename extensions themselves, separate from filenames. -
Field Summary
Modifier and TypeFieldDescriptionstatic final Characters
The characters that may not be used in various file system filenames.static final Characters
The characters that may not be used as the last character of various file system filenames.static final String
The special name "." indicating the current directory.static final char
The prefix used by Unix to designate a dotfile, which is usually hidden.static final char
The character to use for escaping reserved characters.static final char
The character used to separate an extension from the rest of a filename.static final String
The special name ".." indicating the parent directory.static final Characters
The characters that may not be used in POSIX filenames.static final Characters
The characters that may not be used in Windows filenames.static final Characters
The characters that may not be used as the last character of Windows filenames. -
Method Summary
Modifier and TypeMethodDescriptionstatic String
addExtension
(String filename, String extension) Adds the given extension to a filename and returns the new filename with the new extension.static String
appendBase
(String filename, CharSequence charSequence) Appends a given string to the end of a filename before the extension, if any.static String
appendBaseFilename
(String filename, CharSequence charSequence) Deprecated.static String
changeBase
(String filename, String base) Changes the base filename, preserving the extension(s), if any.static String
changeExtension
(String filename, String extension) Changes the last extension of a filename and returns a new filename with the new extension.static Comparator<CharSequence>
Returns a general filename comparator with neutral comparison across locales.static Comparator<CharSequence>
comparator
(Collator collator) Returns a filename comparator using the given collator.static Comparator<CharSequence>
comparator
(Locale locale) Returns a filename comparator for the given locale.static String
decodeFilename
(String filename) Unescapes all characters in a string that are encoded using'^'
as an escape character followed by two hex digits.static String
encodeCrossPlatformFilename
(String filename) Escape all reserved filename characters to a two-digit uppercase hex representation using'^'
as an escape character so that the filename can be used across operating systems.static String
encodeFilename
(String filename) Escape all reserved filename characters to a two-digit uppercase hex representation using'^'
as an escape character.static String
encodeFilename
(String filename, Characters reservedCharacters, Characters reservedFinalCharacters) Escape all reserved filename characters to a two-digit uppercase hex representation using'^'
as an escape character.extensions
(CharSequence filename) Returns all the possible extensions of a filename, from the most specific to the most general.findExtension
(String filename) Extracts the extension from a filename.static String
Retrieves a base filename with no extensionsstatic String
getBaseFilename
(String filename) Deprecated.to be removed in favor ofgetBase(String)
.static Pattern
getBaseFilenamePattern
(String baseFilename) Creates a pattern for matching a base filename (the given base name followed by one or more filename extensions).static String
getExtension
(String filename) Deprecated.to be removed in favor offindExtension(String)
.getExtensions
(CharSequence filename) Returns all the possible extensions of a filename, from the most specific to the most general.static boolean
hasExtension
(String filename, String extension) Determines whether a given filename has the indicated extension.static boolean
isCrossPlatformFilename
(String string) Checks to ensure that a particular string is a valid filename across operating systems.static boolean
isDotfileFilename
(CharSequence filename) Determines whether the filename is for a so-called "dotfile", beginning with a dot but including at least one other character.static boolean
isSpecialName
(String name) Determines if a filename is considered "special" (such as a parent directory designation) on file systems in general and cannot therefore be used as a normal filename.static boolean
isValidFilename
(String string) Checks to ensure that a particular string is a valid filename for the operating system.static boolean
isValidFilename
(String string, Characters reservedCharacters, Characters reservedFinalCharacters) Checks to ensure that a particular string is a valid filename.static String
removeExtension
(String filename) Removes the last extension, if any, of a filename and returns a new filename with no extension.static String
setExtension
(String filename, String extension) Adds the extension, if any, to a filename and returns the new filename.
-
Field Details
-
DOTFILE_PREFIX
public static final char DOTFILE_PREFIXThe prefix used by Unix to designate a dotfile, which is usually hidden. -
EXTENSION_SEPARATOR
public static final char EXTENSION_SEPARATORThe character used to separate an extension from the rest of a filename.- See Also:
-
CURRENT_DIRECTORY_NAME
The special name "." indicating the current directory.- API Note:
- This is not necessarily a special name on every implementation of a
FileSystem
. See further discussion on Stack Overflow. - See Also:
-
PARENT_DIRECTORY_NAME
The special name ".." indicating the parent directory.- API Note:
- This is not necessarily a special name on every implementation of a
FileSystem
. See further discussion on Stack Overflow. - See Also:
-
ESCAPE_CHAR
public static final char ESCAPE_CHARThe character to use for escaping reserved characters.This is a somewhat arbitrary, proprietary escape character. It does not reflect any existing convention.
Java automatically converts
'%'
in URIs and does not correctly access file URIs containing'#'
, so neither of these characters can be used as an escape character.Note that, as
'^'
is not a valid URI character, it will be escaped again using'%'
if such a filename is included in a URI.- See Also:
-
POSIX_RESERVED_CHARACTERS
The characters that may not be used in POSIX filenames. -
WINDOWS_RESERVED_CHARACTERS
The characters that may not be used in Windows filenames. -
WINDOWS_RESERVED_FINAL_CHARACTERS
The characters that may not be used as the last character of Windows filenames. -
CROSS_PLATFORM_RESERVED_CHARACTERS
The characters that may not be used in various file system filenames. -
CROSS_PLATFORM_RESERVED_FINAL_CHARACTERS
The characters that may not be used as the last character of various file system filenames.
-
-
Method Details
-
isSpecialName
Determines if a filename is considered "special" (such as a parent directory designation) on file systems in general and cannot therefore be used as a normal filename.- API Note:
- If this method returns
true
for a name, it does not necessarily mean that the name is considered special on every implementation of aFileSystem
. See further discussion on Stack Overflow. - Parameters:
name
- The filename to test.- Returns:
true
if the given name is special and should not be used as a filename.- See Also:
-
comparator
Returns a general filename comparator with neutral comparison across locales. A filename comparator sorts the base filename and extension separately.- Implementation Specification:
- This implementation uses a collator that takes into account differences in case and accents.
- Returns:
- A neutral filename comparator for the root locale.
- See Also:
-
comparator
Returns a filename comparator for the given locale. A filename comparator sorts the base filename and extension separately.- Implementation Specification:
- This implementation uses a collator that takes into account differences in case and accents.
- Parameters:
locale
- The locale to use for comparison.- Returns:
- A filename comparator for the given locale.
-
comparator
Returns a filename comparator using the given collator. A filename comparator sorts the base filename and extension separately.- Parameters:
collator
- The collator to use for comparisons.- Returns:
- A filename comparator using the given collator.
-
isCrossPlatformFilename
Checks to ensure that a particular string is a valid filename across operating systems.- Parameters:
string
- The string of characters which may represent a filename.- Returns:
true
if the string contains no illegal filename characters.
-
isDotfileFilename
Determines whether the filename is for a so-called "dotfile", beginning with a dot but including at least one other character. This method does not consider"."
and".."
to be dotfile filenames.- Implementation Specification:
- The current implementation currently does not consider whether the filename contains slashes of any sort, assuming that the string is actually a filename if it is non-empty.
- Parameters:
filename
- The filename to check.- Returns:
true
if the filename is considered a dotfile.- Throws:
IllegalArgumentException
- if the filename is the empty string, which is not a valid filename.- See Also:
-
isValidFilename
Checks to ensure that a particular string is a valid filename for the operating system.The reserved characters of the current operating system will be used.
- Parameters:
string
- The string of characters which may represent a filename.- Returns:
true
if the string contains no illegal filename characters.
-
isValidFilename
public static boolean isValidFilename(String string, Characters reservedCharacters, Characters reservedFinalCharacters) Checks to ensure that a particular string is a valid filename.- Parameters:
string
- The string of characters which may represent a filename.reservedCharacters
- The reserved characters which should be encoded.reservedFinalCharacters
- The characters that should be encoded if they appear in the final position of the filename, ornull
if the final character doesn't have to meet special rules.- Returns:
true
if the string contains no reserved filename characters.
-
encodeCrossPlatformFilename
Escape all reserved filename characters to a two-digit uppercase hex representation using'^'
as an escape character so that the filename can be used across operating systems.Note that this encodes path separators, and therefore this method should only be called on filenames, not paths.
- Parameters:
filename
- The filename string to be encoded.- Returns:
- The string modified to be a filename.
- See Also:
-
encodeFilename
Escape all reserved filename characters to a two-digit uppercase hex representation using'^'
as an escape character.Note that this encodes path separators, and therefore this method should only be called on filenames, not paths.
The filename is encoded using the reserved characters of the current operating system.
- Parameters:
filename
- The filename string to be encoded.- Returns:
- The string modified to be a filename.
- See Also:
-
encodeFilename
public static String encodeFilename(String filename, Characters reservedCharacters, Characters reservedFinalCharacters) Escape all reserved filename characters to a two-digit uppercase hex representation using'^'
as an escape character.Note that this encodes path separators, and therefore this method should only be called on filenames, not paths.
- Parameters:
filename
- The filename string to be encoded.reservedCharacters
- The reserved characters which should be encoded.reservedFinalCharacters
- The characters that should be encoded if they appear in the final position of the filename, ornull
if the final character doesn't have to meet special rules.- Returns:
- The string modified to be a filename.
- See Also:
-
decodeFilename
Unescapes all characters in a string that are encoded using'^'
as an escape character followed by two hex digits.- Parameters:
filename
- The filename string to be decoded.- Returns:
- The filename string decoded back to a normal string.
- See Also:
-
getBaseFilenamePattern
Creates a pattern for matching a base filename (the given base name followed by one or more filename extensions).- Parameters:
baseFilename
- The filename base name to match.- Returns:
- A pattern for for matching filenames against the given base name.
-
appendBase
Appends a given string to the end of a filename before the extension, if any.- API Note:
- This is useful for forming a locale-aware filename, such as
test_fr.txt
fromtest.txt
., Here "base filename" refers to the filename with all extensions removed. That is bothexample.bar
andexample.foo.bar
would result in a base filename ofexample
. - Parameters:
filename
- The filename that may contain an extension.charSequence
- The characters to append to the filename.- Returns:
- A filename with the given character sequence appended before the filename extension, if any.
-
appendBaseFilename
Deprecated.to be removed in favor ofappendBase(String, CharSequence)
.Appends a given string to the end of a filename before the extension, if any.- API Note:
- This is useful for forming a locale-aware filename, such as
test_fr.txt
fromtest.txt
., Here "base filename" refers to the filename with all extensions removed. That is bothexample.bar
andexample.foo.bar
would result in a base filename ofexample
. - Parameters:
filename
- The filename that may contain an extension.charSequence
- The characters to append to the filename.- Returns:
- A filename with the given character sequence appended before the filename extension, if any.
-
changeBase
Changes the base filename, preserving the extension(s), if any.- API Note:
- Here "base filename" refers to the filename with all extensions removed. That is both
example.bar
andexample.foo.bar
would result in a base filename ofexample
. - Parameters:
filename
- The filename to examine.base
- The new base to set.- Returns:
- The filename with the new base.
- Throws:
NullPointerException
- if the given filename and/or base isnull
.IllegalArgumentException
- if the given filename and/or base is empty.
-
getBase
Retrieves a base filename with no extensions- API Note:
- Here "base filename" refers to the filename with all extensions removed. That is both
example.bar
andexample.foo.bar
would result in a base filename ofexample
. - Parameters:
filename
- The filename that may contain an extension.- Returns:
- A filename with all extensions, if any, removed.
-
getBaseFilename
Deprecated.to be removed in favor ofgetBase(String)
.Retrieves a base filename with no extensions- API Note:
- Here "base filename" refers to the filename with all extensions removed. That is both
example.bar
andexample.foo.bar
would result in a base filename ofexample
. - Parameters:
filename
- The filename that may contain an extension.- Returns:
- A filename with all extensions, if any, removed.
-
extensions
Returns all the possible extensions of a filename, from the most specific to the most general.For example for the filename
example.foo.bar
the following would be returned in order:foo.bar
bar
- Parameters:
filename
- The filename for which extensions should be returned.- Returns:
- A stream of extensions of the given filename.
-
getExtensions
Returns all the possible extensions of a filename, from the most specific to the most general.For example for the filename
example.foo.bar
the following would be returned in order:foo.bar
bar
- Parameters:
filename
- The filename for which extensions should be returned.- Returns:
- An iterable to iterate over the extensions of the given filename.
- See Also:
-
addExtension
Adds the given extension to a filename and returns the new filename with the new extension. The name is not checked to see if it currently has an extension.- Implementation Note:
- This implementation currently allows an extension with the
.
delimiter, but it may be prohibited in the future., This implementation currently allows an empty filename, but may be prohibited in the future. - Parameters:
filename
- The filename name to which to add an extension.extension
- The extension to add.- Returns:
- The name with the new extension.
- Throws:
NullPointerException
- if the given extension isnull
.
-
changeExtension
Changes the last extension of a filename and returns a new filename with the new extension. If the filename does not currently have an extension, one will be added.- Parameters:
filename
- The filename to examine.extension
- The extension to set, ornull
if the extension should be removed.- Returns:
- The filename with the new extension.
- Throws:
IllegalArgumentException
- if the filename is empty.
-
findExtension
Extracts the extension from a filename.- Parameters:
filename
- The filename to examine.- Returns:
- The extension of the name (not including '.'), which may not be present.
-
hasExtension
Determines whether a given filename has the indicated extension.This methods compares filenames on an ASCII case-insensitive basis. As such testing for the extension
bar
will returntrue
both for the filenamefoo.bar
and also for the filenamefoo.BAR
.- Implementation Specification:
- This implementation delegates to
Filenames.Extensions.equals(String, String)
. - Parameters:
filename
- The filename to check.extension
- The extension to match.- Returns:
true
if the given filename has the indicated extension in any allowed form.
-
getExtension
Deprecated.to be removed in favor offindExtension(String)
.Extracts the extension from a filename.- Parameters:
filename
- The filename to examine.- Returns:
- The extension of the name (not including '.'), which may not be present.
-
removeExtension
Removes the last extension, if any, of a filename and returns a new filename with no extension. This is a convenience method that delegates tochangeExtension(String, String)
.- Parameters:
filename
- The name to examine.- Returns:
- The name with no extension.
-
setExtension
Adds the extension, if any, to a filename and returns the new filename. This is a convenience method that delegates toaddExtension(String, String)
if a non-null
extension is given.- Parameters:
filename
- The filename to examine.extension
- The extension to add, ornull
if no extension should be added.- Returns:
- The name with the new extension, if any.
-
appendBase(String, CharSequence)
.