Class FilenameUtils

java.lang.Object
eu.bandm.tools.util.files.FilenameUtils

@PropertyDependency("file.separator") public class FilenameUtils extends Object
Static methods for filename analysis and manipulation.
  • Field Details

    • PROPERTY_NAME_FILE_SEPARATOR

      public static final String 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

      public static String appendTrailingSeparator(String in)
      Appends the OS-specific directory level separator, if not already trailing.
      Parameters:
      in - the filename to be decorated
      Returns:
      the decorated filename
    • removeTrailingSeparator

      public static String removeTrailingSeparator(String in)
      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 processed
      placeholder - to be replaced
      replacement - 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 processed
      placeholder - to be replaced
      replacement - to be inserted instead of one single placeholder
      Returns:
      the processed filename, or null in case of error
    • getExtension

      @Opt public static @Opt String getExtension(File f)
      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

      @Opt public static @Opt String getExtension(String fn)
      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

      public static String stripExtension(File f)
      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

      public static String stripExtension(String fn)
      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

      public static String multiply(String fnam, String insert)
      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 processed
      insert - the string to insert
      Returns:
      the expanded filename.
    • replaceExtension

      public static String replaceExtension(File f, @Opt @Opt String e) throws IOException
      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 name
      IOException - if the canonical path of the file cannot be read in its local name
    • replaceExtension

      public static String replaceExtension(String fn, @Opt @Opt String e)
      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

      public static String replaceWindowsFileSeparator(String fn)
      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 sets File.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

      public static String anonymiseLocalFile(String path)
      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

      public static File nestedFile(File start, String... names)
      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.