Class FilenameUtils

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

public class FilenameUtils extends Object
Static methods for file name analysis and manipulation.
  • Method Details

    • replaceUniquePlaceholder

      @Opt public static @Opt String replaceUniquePlaceholder(String in, String placeholder, String replacement)
      Replaces in "in" the only occurence of "placeholder" by "replacement". Returns null if no such occurence or more than one occurence.
    • 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.
    • getExtension

      @Opt public static @Opt String getExtension(String fn)
      Returns the suffix after the last occurence of ".", or null if no such.
    • 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.
    • stripExtension

      public static String stripExtension(String fn)
      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 file name. If there are ".", then insert it directly before the last of these. Otherwise append it to the end.
    • replaceExtension

      public static String replaceExtension(File f, @Opt @Opt String e) throws IOException
      Replace the extension by a different one, or remove it completely.
      Parameters:
      f - 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
    • replaceExtension

      public static String replaceExtension(String fn, @Opt @Opt String e)
      Replace the extension by a different one, or remove it completely.
      Parameters:
      fn - must contain a dot "."
      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
    • replaceWindowsFileSeparator

      public static String replaceWindowsFileSeparator(String fn)
      Makes a valid URL type and unix type file name from a windows file identifier. Can be applied to a file name 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.