Class Filenames
- java.lang.Object
-
- com.globalmentor.io.Filenames
-
public class Filenames extends java.lang.Object
Utilities for working with filenames, whether from files, paths, or URIs for example.- Author:
- Garret Wilson
-
-
Field Summary
Fields Modifier and Type Field Description static Characters
CROSS_PLATFORM_RESERVED_CHARACTERS
The characters that may not be used in various file system filenames.static Characters
CROSS_PLATFORM_RESERVED_FINAL_CHARACTERS
The characters that may not be used as the last character of various file system filenames.static char
DOTFILE_PREFIX
The prefix used by Unix to designate a dotfile, which is usually hidden.static char
ESCAPE_CHAR
The character to use for escaping reserved characters.static char
EXTENSION_SEPARATOR
The character used to separate an extension from the rest of a filename.static Characters
POSIX_RESERVED_CHARACTERS
The characters that may not be used in POSIX filenames.static Characters
WINDOWS_RESERVED_CHARACTERS
The characters that may not be used in Windows filenames.static Characters
WINDOWS_RESERVED_FINAL_CHARACTERS
The characters that may not be used as the last character of Windows filenames.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.lang.String
addExtension(java.lang.String filename, java.lang.String extension)
Adds the given extension to a filename and returns the new filename with the new extension.static java.lang.String
appendBaseFilename(java.lang.String filename, java.lang.CharSequence charSequence)
Appends a given string to the end of a filename before the extension, if any.static java.lang.String
changeExtension(java.lang.String filename, java.lang.String extension)
Changes the last extension of a filename and returns a new filename with the new extension.static java.lang.String
decodeFilename(java.lang.String filename)
Unescapes all characters in a string that are encoded using'^'
as an escape character followed by two hex digits.static java.lang.String
encodeCrossPlatformFilename(java.lang.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 java.lang.String
encodeFilename(java.lang.String filename)
Escape all reserved filename characters to a two-digit uppercase hex representation using'^'
as an escape character.static java.lang.String
encodeFilename(java.lang.String filename, Characters reservedCharacters, Characters reservedFinalCharacters)
Escape all reserved filename characters to a two-digit uppercase hex representation using'^'
as an escape character.static java.util.stream.Stream<java.lang.String>
extensions(java.lang.CharSequence filename)
Returns all the possible extensions of a filename, from the most specific to the most general.static java.lang.String
getBaseFilename(java.lang.String filename)
Retrieves a base filename with no extensionsstatic java.util.regex.Pattern
getBaseFilenamePattern(java.lang.String baseFilename)
Creates a pattern for matching a base filename (the given base name followed by one or more filename extensions).static java.lang.String
getExtension(java.lang.String filename)
Extracts the extension from a filename.static java.lang.Iterable<java.lang.String>
getExtensions(java.lang.CharSequence filename)
Returns all the possible extensions of a filename, from the most specific to the most general.static boolean
isCrossPlatformFilename(java.lang.String string)
Checks to ensure that a particular string is a valid filename across operating systems.static boolean
isDotfileFilename(java.lang.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
isValidFilename(java.lang.String string)
Checks to ensure that a particular string is a valid filename for the operating system.static boolean
isValidFilename(java.lang.String string, Characters reservedCharacters, Characters reservedFinalCharacters)
Checks to ensure that a particular string is a valid filename.static java.lang.String
removeExtension(java.lang.String filename)
Removes the last extension, if any, of a filename and returns a new filename with no extension.static java.lang.String
setExtension(java.lang.String filename, java.lang.String extension)
Adds the extension, if any, to a filename and returns the new filename.
-
-
-
Field Detail
-
DOTFILE_PREFIX
public static final char DOTFILE_PREFIX
The prefix used by Unix to designate a dotfile, which is usually hidden.
-
EXTENSION_SEPARATOR
public static final char EXTENSION_SEPARATOR
The character used to separate an extension from the rest of a filename.- See Also:
- Constant Field Values
-
ESCAPE_CHAR
public static final char ESCAPE_CHAR
The 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:
- Constant Field Values
-
POSIX_RESERVED_CHARACTERS
public static final Characters POSIX_RESERVED_CHARACTERS
The characters that may not be used in POSIX filenames.
-
WINDOWS_RESERVED_CHARACTERS
public static final Characters WINDOWS_RESERVED_CHARACTERS
The characters that may not be used in Windows filenames.
-
WINDOWS_RESERVED_FINAL_CHARACTERS
public static final Characters WINDOWS_RESERVED_FINAL_CHARACTERS
The characters that may not be used as the last character of Windows filenames.
-
CROSS_PLATFORM_RESERVED_CHARACTERS
public static final Characters CROSS_PLATFORM_RESERVED_CHARACTERS
The characters that may not be used in various file system filenames.
-
CROSS_PLATFORM_RESERVED_FINAL_CHARACTERS
public static final Characters CROSS_PLATFORM_RESERVED_FINAL_CHARACTERS
The characters that may not be used as the last character of various file system filenames.
-
-
Method Detail
-
getBaseFilenamePattern
public static java.util.regex.Pattern getBaseFilenamePattern(@Nonnull java.lang.String baseFilename)
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.
-
appendBaseFilename
public static java.lang.String appendBaseFilename(java.lang.String filename, java.lang.CharSequence charSequence)
Appends a given string to the end of a filename before the extension, if any. This is useful for forming a locale-aware filename, such astest_fr.txt
fromtest.txt
.- Parameters:
filename
- The filename that may contain an extension.charSequence
- The characters to append to the filename.- Returns:
- A filename with the given string appended before the filename extension, if any.
-
getBaseFilename
public static java.lang.String getBaseFilename(java.lang.String filename)
Retrieves a base filename with no extensions- Parameters:
filename
- The filename that may contain an extension.- Returns:
- A filename with all extensions, if any, removed.
-
isCrossPlatformFilename
public static boolean isCrossPlatformFilename(java.lang.String string)
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
public static boolean isDotfileFilename(@Nonnull java.lang.CharSequence filename)
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:
java.lang.IllegalArgumentException
- if the filename is the empty string, which is not a valid filename.- See Also:
- Why do some file/folder names on Windows have a dot in front of them?,
Dotfiles,
DOTFILE_PREFIX
-
isValidFilename
public static boolean isValidFilename(java.lang.String string)
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(java.lang.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
public static java.lang.String encodeCrossPlatformFilename(java.lang.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.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:
CROSS_PLATFORM_RESERVED_CHARACTERS
,CROSS_PLATFORM_RESERVED_FINAL_CHARACTERS
,ESCAPE_CHAR
,CharSequences.escapeHex(CharSequence, Characters, Characters, int, char, int, Case)
,isValidFilename(String, Characters, Characters)
-
encodeFilename
public static java.lang.String encodeFilename(java.lang.String filename)
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:
WINDOWS_RESERVED_FINAL_CHARACTERS
,POSIX_RESERVED_CHARACTERS
,ESCAPE_CHAR
,CharSequences.escapeHex(CharSequence, Characters, Characters, int, char, int, Case)
,isValidFilename(String, Characters, Characters)
-
encodeFilename
public static java.lang.String encodeFilename(java.lang.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:
ESCAPE_CHAR
,CharSequences.escapeHex(CharSequence, Characters, Characters, int, char, int, Case)
,isValidFilename(String, Characters, Characters)
-
decodeFilename
public static java.lang.String decodeFilename(java.lang.String filename)
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:
ESCAPE_CHAR
,CharSequences.unescapeHex(CharSequence, char, int)
-
extensions
public static java.util.stream.Stream<java.lang.String> extensions(java.lang.CharSequence filename)
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
public static java.lang.Iterable<java.lang.String> getExtensions(java.lang.CharSequence filename)
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:
FilenameExtensionIterator
-
addExtension
public static java.lang.String addExtension(java.lang.String filename, java.lang.String extension)
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.This method currently allows an extension with the
.
delimiter, but it 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:
java.lang.NullPointerException
- if the given extension isnull
.
-
changeExtension
public static java.lang.String changeExtension(java.lang.String filename, java.lang.String extension)
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:
java.lang.IllegalArgumentException
- If the name is empty, or if the name is just a "/".
-
getExtension
public static java.lang.String getExtension(java.lang.String filename)
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
public static java.lang.String removeExtension(java.lang.String filename)
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
public static java.lang.String setExtension(java.lang.String filename, java.lang.String extension)
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.
-
-