Class FileUtilities


public final class FileUtilities
Static utility methods for working with file system paths and URIs, including recursive file lookup, extension/name parsing, parent directory resolution and human-readable byte size formatting.
  • Method Details

    • findFilesByExtension

      public static List<String> findFilesByExtension(List<String> fileNames, Path dir, String extension)
      Recursively collects all files under dir whose absolute path ends with the supplied extension.
      Parameters:
      fileNames - the (mutable) list that receives the matching file paths
      dir - the directory to search
      extension - the file extension to look for
      Returns:
      fileNames for chaining
    • findFiles

      public static List<String> findFiles(List<String> fileNames, Path dir, String... files)
      Recursively collects all files under dir whose absolute path ends with one of the supplied files names.
      Parameters:
      fileNames - the (mutable) list that receives the matching file paths
      dir - the directory to search
      files - the file name suffixes to look for
      Returns:
      fileNames for chaining
    • getExtension

      public static String getExtension(Path file)
      Returns the extension of the supplied file path (without the leading dot), or an empty string if the path has no extension.
      Parameters:
      file - the file path
      Returns:
      the extension, or ""
    • getExtension

      public static String getExtension(String path)
      Returns the extension of the supplied path string (without the leading dot), or an empty string if the path has no extension.
      Parameters:
      path - the path string
      Returns:
      the extension, or ""
    • getFileName

      public static String getFileName(URL path)
      Returns the file name (without extension) of the supplied URL.
      Parameters:
      path - the URL
      Returns:
      the file name without extension
    • getFileName

      public static String getFileName(String path)
      Returns the file name (without extension) of the supplied path string.
      Parameters:
      path - the path string
      Returns:
      the file name without extension
    • getFileName

      public static String getFileName(String path, boolean extension)
      Returns the file name of the supplied path string, optionally including the extension.
      Parameters:
      path - the path string
      extension - true to keep the extension; false to strip it
      Returns:
      the file name
    • getParentDirPath

      public static String getParentDirPath(String uri)
    • getParentDirPath

      public static String getParentDirPath(URI uri)
    • combine

      public static String combine(String basePath, String... paths)
      This method combines the specified basepath with the parts provided as arguments. The output will use the path separator of the current system;
      Parameters:
      basePath - The base path for the combined path.
      paths - The parts of the path to be constructed.
      Returns:
      The combined path.
    • humanReadableByteCount

      public static String humanReadableByteCount(long bytes)
    • humanReadableByteCount

      public static String humanReadableByteCount(long bytes, boolean decimal)