Module id.xfunction

Class XFiles

Object
XFiles

public class XFiles extends Object
Additions to standard java.nio.file.Files
  • Field Details

    • TEMP_FOLDER

      public static final Optional<Path> TEMP_FOLDER
      System temporary folder location if it was found
  • Constructor Details

    • XFiles

      public XFiles()
  • Method Details

    • deleteRecursively

      public static void deleteRecursively(Path dir) throws IOException
      Deletes directory recursively with all files and sub-directories
      Throws:
      IOException
    • copyRecursively

      public static void copyRecursively(Path source, Path destination) throws IOException
      Copies directory recursively with all files and sub-directories inside.
      Parameters:
      source - file or directory to copy from
      Throws:
      IOException
    • containsAll

      public static boolean containsAll(Path a, Path b) throws IOException
      Checks, non recursively, if all files of folder A exists in folder B and their content is equal (byte-to-byte comparison).
      Parameters:
      a - folder A
      b - folder B
      Throws:
      IOException
    • containsAllRecursively

      public static boolean containsAllRecursively(Path a, Path b) throws IOException
      Checks, recursively, if all files of folder A and its subfolders exists in folder B and their content is equal (byte-to-byte comparison).
      Parameters:
      a - folder A
      b - folder B
      Throws:
      IOException
    • containsAll

      public static boolean containsAll(Path a, Path b, int maxDepth) throws IOException
      Parameters:
      maxDepth - the maximum number of directory levels to visit
      Throws:
      IOException
      See Also:
    • watchForLineInFile

      public static Future<String> watchForLineInFile(Path filePath, Predicate<String> matchPredicate, Duration pollDelay) throws IOException, InterruptedException
      Setup a watcher on a file for a particular line.

      It polls file with given delay for all new incoming lines. Once it finds a line which matches the predicate it completes the future with that line.

      Initially this function was implemented with WatchService. This was more efficient since we avoided unnecessary polls when file is not changed. Unfortunately in certain conditions WatchService could stuck waiting for events forever although the file was changed already multiple of times. Querying the file size somehow affects this and unblocks the events.

      Such behavior was observed in Windows10, Java 17 with log file which was actively written but WatchService was not emiting anything.

      Throws:
      IOException
      InterruptedException
    • isContentEqual

      public static boolean isContentEqual(File a, File b) throws IOException
      Compares two files byte-to-byte
      Throws:
      IOException
    • findFilesRecursively

      public static Stream<Path> findFilesRecursively(Path root, Predicate<Path> fileFilter) throws IOException
      Expands to:
      
       Files.find(root, Integer.MAX_VALUE, (p, a) -> fileFilter.test(p));
       
      Throws:
      IOException