Class SafeFiles


  • public final class SafeFiles
    extends Object
    Provides methods to operate on files using Path that avoid leaking file handles.
    • Method Detail

      • listAll

        public static List<Path> listAll​(Path dir)
        Lists the elements in the specified directory without recursing into subdirectories.

        This is a safe wrapper for Files.list(Path).

        Parameters:
        dir - the path to list files
        Returns:
        the list of paths in the specified directory
        Throws:
        SecurityException - if access is rejected by a security manager
        UncheckedIOException - if an IO error occurs
      • list

        public static <T> T list​(Path dir,
                                 Function<Stream<Path>,​T> function)
        Streams the elements in the specified directory without recursing into subdirectories.

        This is a safe wrapper for Files.list(Path).

        Type Parameters:
        T - the type of the result
        Parameters:
        dir - the path to list files
        function - the function to apply to the stream
        Returns:
        the result of the function
        Throws:
        SecurityException - if access is rejected by a security manager
        UncheckedIOException - if an IO error occurs
      • walkAll

        public static List<Path> walkAll​(Path dir)
        Lists the elements in the specified directory, recursing depth-first into subdirectories.

        This is a safe wrapper for Files.walk(Path, FileVisitOption...). It recurses into all subdirectories and does not follow links.

        Parameters:
        dir - the start path to list files from
        Returns:
        the list of paths in the specified directory
        Throws:
        IllegalArgumentException - if maxDepth is negative
        SecurityException - if access is rejected by a security manager
        UncheckedIOException - if an IO error occurs
      • walk

        public static <T> T walk​(Path dir,
                                 int maxDepth,
                                 Function<Stream<Path>,​T> function,
                                 FileVisitOption... options)
        Lists the elements in the specified directory, recursing depth-first into subdirectories.

        This is a safe wrapper for Files.walk(Path, int, FileVisitOption...).

        Type Parameters:
        T - the type of the result
        Parameters:
        dir - the start path to list files from
        maxDepth - the maximum depth to recurse to, one for one-level and so on
        function - the function to apply to the stream
        options - the options
        Returns:
        the result of the function
        Throws:
        IllegalArgumentException - if maxDepth is negative
        SecurityException - if access is rejected by a security manager
        UncheckedIOException - if an IO error occurs
      • linesAll

        public static List<String> linesAll​(Path file)
        Loads the specified file as a list of lines using UTF-8.

        This is a safe wrapper for Files.lines(Path).

        Parameters:
        file - the path to read
        Returns:
        the list of lines in the specified file
        Throws:
        SecurityException - if access is rejected by a security manager
        UncheckedIOException - if an IO error occurs
      • lines

        public static <T> T lines​(Path file,
                                  Function<Stream<String>,​T> function)
        Streams the lines in the specified file using UTF-8.

        This is a safe wrapper for Files.lines(Path).

        Type Parameters:
        T - the type of the result
        Parameters:
        file - the path to read
        function - the function to apply to the stream
        Returns:
        the result of the function
        Throws:
        SecurityException - if access is rejected by a security manager
        UncheckedIOException - if an IO error occurs