Package eu.bandm.tools.util.files
Class FilenameUtils
java.lang.Object
eu.bandm.tools.util.files.FilenameUtils
Static methods for filename analysis and manipulation.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringThe property name under which to find the system property for the current os' directory level separator in file pahts. -
Method Summary
Modifier and TypeMethodDescriptionstatic StringanonymiseLocalFile(String path) Heuristics to exclude local, private file system structure from published sources.static StringAppends the OS-specific directory level separator, if not already trailing.getExtension(File f) Returns the suffix after the last occurence of"."in the file's local name, or null if no such.getExtension(String fn) Returns the suffix after the last occurence of".", or null if no such exists.static StringInsert a string into a filename.static FilenestedFile(File start, String... names) Constructs a file object from a root file and a sequence of names.static StringRemoves a trailing OS-specific directory level separator, if present.static StringreplaceExtension(File f, @Opt String e) Replace the extension in the canonical path of a file by a different string value, or remove it completely.static StringreplaceExtension(String fn, @Opt String e) Replace the extension by a different one, or remove it completely.replaceFirstPlaceholder(String in, String placeholder, String replacement) Replaces in "in" the first occurence of "placeholder" by "replacement".replaceUniquePlaceholder(String in, String placeholder, String replacement) Replaces" the only occurence of a placeholder by a replacement.static StringMakes a valid URL-style and unix-style filename from a windows file identifier.static StringReturns the prefix of the file's local name before the last occurence of".", or the whole string if no such.static StringstripExtension(String fn) Returns the prefix before the last occurence of".", or the whole string if no such.
-
Field Details
-
PROPERTY_NAME_FILE_SEPARATOR
The property name under which to find the system property for the current os' directory level separator in file pahts.- See Also:
-
-
Method Details
-
appendTrailingSeparator
Appends the OS-specific directory level separator, if not already trailing.- Parameters:
in- the filename to be decorated- Returns:
- the decorated filename
-
removeTrailingSeparator
Removes a trailing OS-specific directory level separator, if present.- Parameters:
in- the filename to be pruned- Returns:
- the pruned filename
-
replaceUniquePlaceholder
@Opt public static @Opt String replaceUniquePlaceholder(String in, String placeholder, String replacement) Replaces" the only occurence of a placeholder by a replacement. Returns null if no such occurence or more than one occurence.- Parameters:
in- the filename to be processedplaceholder- to be replacedreplacement- to be inserted instead of one single placeholder- Returns:
- the processed filename, or null in case of error
-
replaceFirstPlaceholder
@Opt public static @Opt String replaceFirstPlaceholder(String in, String placeholder, String replacement) Replaces in "in" the first occurence of "placeholder" by "replacement". Returns null if no such occurence.- Parameters:
in- the filename to be processedplaceholder- to be replacedreplacement- to be inserted instead of one single placeholder- Returns:
- the processed filename, or null in case of error
-
getExtension
Returns the suffix after the last occurence of"."in the file's local name, or null if no such.- Parameters:
f- the file to be processed- Returns:
- the suffix after the last occurence of
"."in the file's local name, or null if no such.
-
getExtension
Returns the suffix after the last occurence of".", or null if no such exists.- Parameters:
fn- the filename to be processed- Returns:
- the suffix after the last occurence of
".", or null if no such exists.
-
stripExtension
Returns the prefix of the file's local name before the last occurence of".", or the whole string if no such.- Parameters:
f- the file to be processed- Returns:
- the prefix of the file's local name
before the last occurence of
".", or the whole string if no such.
-
stripExtension
Returns the prefix before the last occurence of".", or the whole string if no such.- Parameters:
fn- the filename to be processed- Returns:
- the prefix before the last occurence of
".", or the whole string if no such.
-
multiply
Insert a string into a filename. If there are".", then insert it directly before the last of these. Otherwise append it to the end.- Parameters:
fnam- the filename to be processedinsert- the string to insert- Returns:
- the expanded filename.
-
replaceExtension
Replace the extension in the canonical path of a file by a different string value, or remove it completely.- Parameters:
f- file to process. Must have a dot"."in its last name.e- (maybe null) replaces everything after the last dot.- Returns:
- the full canonical path of the file, with replacement applied. If e==null, then the dot is also removed.
- Throws:
IllegalArgumentException- if the file has no dot"."in its local nameIOException- if the canonical path of the file cannot be read in its local name
-
replaceExtension
Replace the extension by a different one, or remove it completely.- Parameters:
fn- filname to process. Must contain a dot"."e- (maybe null) replaces everything after the last dot after the directory part.- Returns:
- the full canonical path of the file, with replacement applied. If e==null, then the dot is also removed.
- Throws:
IllegalArgumentException- if there is no dot"."in the last (=local) component of the filename.
-
replaceWindowsFileSeparator
Makes a valid URL-style and unix-style filename from a windows file identifier. Can be applied to a filename string which has been constructed by some file choosing GUI widget from the local platform/operating system. Iff this setsFile.separatorChar == backslash, then backslashes are contained in such a string, and are here replaced by slashes.THIS IS ONLY AN APRROXIMATION to the full conversion problem, which is very complicated and involves more transformations. See e.g the discussion on stackoverflow.com/questions/1652483.
- Parameters:
fn- to process- Returns:
- filename with all contained windows file separators replaced by a normal one.
-
anonymiseLocalFile
Heuristics to exclude local, private file system structure from published sources. The function replaces "/home/willy/metatools/" by "$(HOME)/metatools". Currently it works only on Unix/Posix/Linus oss, not on ms-windows.- Parameters:
path- to be anonymised- Returns:
- the anonymised filename
-
nestedFile
Constructs a file object from a root file and a sequence of names.- Parameters:
start- the root file where the construction starts.names- the name components on the descending directory levels below start. When the list is empty, the start parameter is returned.- Returns:
- the file which is reached from start by stepping down the file hierarchy controlled by names.
-