Class FileSystemUtils

java.lang.Object
io.github.connellite.util.FileSystemUtils

public class FileSystemUtils extends Object
Utility methods for working with the file system.
  • Constructor Details

    • FileSystemUtils

      public FileSystemUtils()
  • Method Details

    • deleteRecursively

      public static boolean deleteRecursively(File root)
      Delete the supplied File - for directories, recursively delete any nested directories or files as well.

      Note: Like File.delete(), this method does not throw any exception but rather silently returns false in case of I/O errors. Consider using deleteRecursively(Path) for NIO-style handling of I/O errors, clearly differentiating between non-existence and failure to delete an existing file.

      Parameters:
      root - the root File to delete
      Returns:
      true if the File was successfully deleted, otherwise false
    • deleteRecursively

      public static boolean deleteRecursively(Path root) throws IOException
      Delete the supplied Path - for directories, recursively delete any nested directories or files as well.
      Parameters:
      root - the root Path to delete
      Returns:
      true if the Path existed and was deleted, or false if it did not exist
      Throws:
      IOException - in the case of I/O errors
    • copyRecursively

      public static void copyRecursively(File src, File dest) throws IOException
      Recursively copy the contents of the src file/directory to the dest file/directory.
      Parameters:
      src - the source directory
      dest - the destination directory
      Throws:
      IOException - in the case of I/O errors
    • copyRecursively

      public static void copyRecursively(Path src, Path dest) throws IOException
      Recursively copy the contents of the src file/directory to the dest file/directory.
      Parameters:
      src - the source directory
      dest - the destination directory
      Throws:
      IOException - in the case of I/O errors
    • moveRecursively

      public static void moveRecursively(File src, File dest) throws IOException
      Recursively move the contents of the src file/directory to the dest file/directory.
      Parameters:
      src - the source directory
      dest - the destination directory
      Throws:
      IOException - in the case of I/O errors
    • moveRecursively

      public static void moveRecursively(Path src, Path dest) throws IOException
      Recursively move the contents of the src file/directory to the dest file/directory.
      Parameters:
      src - the source directory
      dest - the destination directory
      Throws:
      IOException - in the case of I/O errors
    • readAllLines

      public static Stream<String> readAllLines(Path path)
      Read the lines of a file and return it as a stream using the platform default charset.
      Parameters:
      path - File path to read
      Returns:
      The lines as a stream
    • readAllLines

      public static Stream<String> readAllLines(Path path, Charset charset)
      Read the lines of a file and return it as a stream.
      Parameters:
      path - File path to read
      charset - Charset to decode bytes as characters
      Returns:
      The lines as a stream
    • readAllBytes

      public static byte[] readAllBytes(InputStream in) throws IOException
      Reads in to EOF and returns the collected bytes. Does not close the stream.
      Parameters:
      in - stream to read; must not be null
      Returns:
      all remaining bytes (empty if the stream is already at EOF)
      Throws:
      IOException - if reading fails