Class FileTools

java.lang.Object
org.savantbuild.io.FileTools

public class FileTools extends Object
File utilities.
  • Constructor Details

    • FileTools

      public FileTools()
  • Method Details

    • createTempPath

      public static Path createTempPath(String prefix, String suffix, boolean deleteOnExit) throws IOException
      Creates a temporary file.
      Parameters:
      prefix - The prefix for the temporary file.
      suffix - The suffix for the temporary file.
      deleteOnExit - If the file should be deleted when the JVM exits.
      Returns:
      The Path of the temporary file.
      Throws:
      IOException - If the create fails.
    • extensionFilter

      public static Predicate<Path> extensionFilter(String extension)
      Returns a Predicate that returns true if the Path has the given extension.
      Parameters:
      extension - The extension.
      Returns:
      The Predicate.
    • extensionMapper

      public static Function<Path,Path> extensionMapper(String original, String target)
      Returns a Function that maps a source path to a target path by changing the extension of the Path.
      Parameters:
      original - The original extension.
      target - The target extension.
      Returns:
      The Function.
    • modifiedFiles

      public static List<Path> modifiedFiles(Path sourceDir, Path outputDir, Predicate<Path> filter, Function<Path,Path> mapper) throws IllegalStateException
      Determines the files that have been modified with respect to the given output directory.
      Parameters:
      sourceDir - The source directory to walk.
      outputDir - The output directory to compare against.
      filter - A predicate that reduces the files that are compared. This should return true for files that are compared and false for those that aren't.
      mapper - A function that maps a source file to a target file.
      Returns:
      The list of modified files.
      Throws:
      IllegalStateException - If the code throws an IOException it is wrapped into an IllegalStateException.
    • prune

      public static void prune(Path path) throws IOException
      Prunes the given path. If the path is a directory, this deletes everything underneath it, but does not traverse across symbolic links, it simply deletes the link. If the path is a file, it is deleted. If the path is a symbolic link, it is unlinked.
      Parameters:
      path - The path to delete.
      Throws:
      IOException - If the prune failed for any reason. This might indicate that the prune was only partially successful.
    • toHexMode

      public static Integer toHexMode(Set<PosixFilePermission> permissions)
      Converts the file permissions to a hex permission value.
      Parameters:
      permissions - The set of POSIX file permissions.
      Returns:
      The hex value.
    • toMode

      public static int toMode(Collection<PosixFilePermission> permissions)

      Converts the file permissions of this FileInfo to a POSIX bit mapped mode. The bit map looks like this:

         1_000_000_001_000_000
       

      The first bit is always set. The next three bits are the set UID bits, the next 3 bits are the set GID bits. The next three bits are the owner permissions (read, write, execute), then the group permissions and finally the user permissions.

      Parameters:
      permissions - The POSIX file permissions.
      Returns:
      The POSIX mode bit map as an integer.
    • toMode

      public static int toMode(int hex)
      Converts a hex value to a POSIX mode. This allows you to use a hex lik 0x777 to represent rwx-rwx-rwx.
      Parameters:
      hex - The hex mode.
      Returns:
      The POSIX mode.
    • toPath

      public static Path toPath(Object object)
      Converts the object to a Path.
      Parameters:
      object - The object.
      Returns:
      The object as a path.
    • toPosixPermissions

      public static Set<PosixFilePermission> toPosixPermissions(int mode)

      Converts the POSIX bit mapped file mode integer to a set of PosixFilePermissions. The bit map looks like this:

         1_000_000_001_000_000
       

      The first bit is always set. The next three bits are the set UID bits, the next 3 bits are the set GID bits. The next three bits are the owner permissions (read, write, execute), then the group permissions and finally the user permissions.

      Parameters:
      mode - The POSIX bit mapped permissions.
      Returns:
      The POSIX mode bit map as an integer.
    • touch

      public static void touch(Path... paths) throws IOException
      Updates the last modified timestamp of each of the given Paths. This effectively "touches" each Path.
      Parameters:
      paths - The Paths to touch.
      Throws:
      IOException - If the update fails.