Class FileUtils


  • public final class FileUtils
    extends Object
    File utilities.
    • Field Detail

      • CURRENT_JAVA_HOME_DIR

        public static final Path CURRENT_JAVA_HOME_DIR
        The Java Home directory for the running JVM.
      • WORKING_DIR

        public static final Path WORKING_DIR
        The working directory.
    • Method Detail

      • fromWorking

        public static Path fromWorking​(Path path)
        Returns the relative path from the working directory for the given path, if possible.
        Parameters:
        path - The path.
        Returns:
        The relative path or the original if it is not within the working directory.
      • ensureDirectory

        public static Path ensureDirectory​(Path path,
                                           FileAttribute<?>... attrs)
        Ensure that the given path is an existing directory, creating it if required.
        Parameters:
        path - The path.
        attrs - The attributes.
        Returns:
        The normalized, absolute directory path.
      • listFiles

        public static List<Path> listFiles​(Path directory,
                                           Predicate<String> fileNameFilter)
        List all files in the given directory that match the given filter. Does not recurse.
        Parameters:
        directory - The directory.
        fileNameFilter - The filter.
        Returns:
        The normalized, absolute file paths.
      • list

        public static List<Path> list​(Path directory)
        List all files and directories in the given directory. Does not recurse.
        Parameters:
        directory - The directory.
        Returns:
        The normalized, absolute file paths.
      • assertDir

        public static Path assertDir​(Path directory)
        Assert that the given path exists and is a directory.
        Parameters:
        directory - The directory.
        Returns:
        The normalized, absolute directory path.
        Throws:
        IllegalArgumentException - If the path does not exist or is not a directory.
      • assertFile

        public static Path assertFile​(Path file)
        Assert that the given path exists and is a file.
        Parameters:
        file - The file.
        Returns:
        The normalized, absolute file path.
        Throws:
        IllegalArgumentException - If the path does not exist or is not a file.
      • assertExists

        public static Path assertExists​(Path path)
        Assert that the given path exists.
        Parameters:
        path - The path.
        Returns:
        The normalized, absolute path.
        Throws:
        IllegalArgumentException - If the path does not exist.
      • deleteDirectory

        public static Path deleteDirectory​(Path directory)
                                    throws IOException
        Deletes the given directory if it exists.
        Parameters:
        directory - The directory.
        Returns:
        The directory.
        Throws:
        IOException - If an error occurs.
      • sizeOf

        public static long sizeOf​(Path path)
        Returns the total size of all files in the given path, including subdirectories.
        Parameters:
        path - The path. May be a file or directory.
        Returns:
        The size, in bytes.
        Throws:
        UncheckedIOException - If an error occurs.
      • lastModifiedTime

        public static long lastModifiedTime​(Path file)
        Returns the last modified time of the given file, in seconds.
        Parameters:
        file - The file.
        Returns:
        The last modified time.
      • fileName

        public static String fileName​(Path file)
        Returns the file name of the given file, as a string.
        Parameters:
        file - The file.
        Returns:
        The name.