Class FileUtils

java.lang.Object
org.aksw.commons.io.util.FileUtils

public class FileUtils extends Object
  • Constructor Details

    • FileUtils

      public FileUtils()
  • Method Details

    • newOutputStream

      public static OutputStream newOutputStream(OutputConfig config) throws IOException
      Attempt to open an output stream to the given file
      Throws:
      IOException
    • deleteRecursivelyIfExists

      public static void deleteRecursivelyIfExists(Path path, com.google.common.io.RecursiveDeleteOption... options) throws IOException
      Throws:
      IOException
    • getFirstExistingAncestor

      public static Path getFirstExistingAncestor(Path path)
      Return the first ancestor of a path that exists. May be the path itself or one if its transitive parents. Returns null on null input. Use Path.relativize(Path) to obtain the folders that would have to be created.
    • deleteFileIfExistsAndThenDeleteEmptyFolders

      public static boolean deleteFileIfExistsAndThenDeleteEmptyFolders(Path path, Path baseFolder, boolean alsoDeleteBaseFolder) throws IOException
      Delete a specific path and then - regardless of deletion outcome -try to delete all empty directories up to a given baseFolder. Empty folders are only deleted if their path starts with the baseFolder The result is the same as of Files.deleteIfExists(Path).
      Throws:
      IOException
    • deleteEmptyFolders

      public static void deleteEmptyFolders(Path path, Path baseFolder, boolean alsoDeleteBase)
      Delete parent folders of 'path' that are descendants of baseFolder (depending on the flag inclusive or exclusive)
    • moveAtomic

      @Deprecated public static void moveAtomic(Path srcFile, Path tgtPath) throws IOException
      Deprecated.
      Best-effort moveAtomic. Use moveAtomicIfPossible.
      Throws:
      IOException
    • moveAtomicIfSupported

      public static void moveAtomicIfSupported(Consumer<String> warnCallback, Path source, Path target) throws IOException
      Throws:
      IOException
    • safeCreate

      public static void safeCreate(Path target, FileUtils.OverwritePolicy overwritePolicy, ThrowingConsumer<OutputStream> writer) throws Exception
      Throws:
      Exception
    • getLastModifiedTimeOrNull

      public static FileTime getLastModifiedTimeOrNull(Path path)
    • safeCreate

      public static void safeCreate(Path target, Function<OutputStream, OutputStream> encoder, FileUtils.OverwritePolicy overwritePolicy, ThrowingConsumer<OutputStream> writer) throws Exception
      Parameters:
      target -
      encoder - A function that can wrap the OutputStream to the target file. May be null.
      overwritePolicy -
      writer - The action that writes to the (possibly encoded) output stream.
      Throws:
      Exception
    • deleteDirectoryIfEmpty

      public void deleteDirectoryIfEmpty(Path path) throws IOException
      Delete a path if it is an empty directory
      Throws:
      IOException
    • listPaths

      public static List<Path> listPaths(Path basePath, String glob) throws IOException
      Return a list of files matching a given glob pattern and a base path
      Parameters:
      basePath -
      glob -
      Returns:
      Throws:
      IOException
    • readObject

      public static <T> T readObject(Path target) throws IOException, ClassNotFoundException
      Throws:
      IOException
      ClassNotFoundException
    • writeObject

      public static void writeObject(Path target, Object obj) throws IOException
      Most basic (and limited) serialization approach using ObjectOutputStream
      Throws:
      IOException
    • sizeOfDirectory

      public static long sizeOfDirectory(Path dirPath) throws IOException
      A java.nio variant of apache commons-io FileUtils.sizeOfDirectory.
      Parameters:
      dirPath -
      Returns:
      Throws:
      IOException
    • sizeOfDirectory

      public static long sizeOfDirectory(Path dirPath, PathMatcher fileMatcher) throws IOException
      A java.nio variant of apache commons-io FileUtils.sizeOfDirectory.
      Parameters:
      dirPath -
      fileMatcher - Predicate to test whether to take a certain file's size into account for the total sum null will match any file.
      Returns:
      Throws:
      IOException
    • ancestors

      public static Stream<Path> ancestors(Path start, boolean reflexive)
    • ancestors

      public static Stream<Path> ancestors(Path start)
    • findInAncestors

      public static Path findInAncestors(Path start, String fileName)
      Look for a file among all ancestor folders
      Parameters:
      start -
      fileName -
      Returns:
    • copyDirectory

      public static void copyDirectory(Path source, Path target, CopyOption... options) throws IOException
      Throws:
      IOException
    • copyDirectoryInternal

      protected static void copyDirectoryInternal(Path source, Path target, CopyOption... options) throws IOException
      Throws:
      IOException
    • copy

      public static long copy(Path source, OutputStream out, IntConsumer contribCallback) throws IOException
      Throws:
      IOException
    • transferTo

      public static long transferTo(OutputStream out, InputStream in, IntConsumer contribCallback) throws IOException
      Throws:
      IOException