Class IOUtils


  • public final class IOUtils
    extends Object
    Utilities for common I/O methods. Borrowed heavily from Lucene (org.apache.lucene.util.IOUtils).
    • Field Detail

      • WINDOWS

        public static final boolean WINDOWS
      • LINUX

        public static final boolean LINUX
      • MAC_OS_X

        public static final boolean MAC_OS_X
    • Method Detail

      • close

        public static void close​(Closeable... objects)
                          throws IOException
        Closes all given Closeables. Some of the Closeables may be null; they are ignored. After everything is closed, the method either throws the first exception it hit while closing with other exceptions added as suppressed, or completes normally if there were no exceptions.
        Parameters:
        objects - objects to close
        Throws:
        IOException
      • close

        public static void close​(Exception e,
                                 Closeable... objects)
                          throws IOException
        Closes all given Closeables. Some of the Closeables may be null; they are ignored. After everything is closed, the method adds any exceptions as suppressed to the original exception, or throws the first exception it hit if Exception is null. If no exceptions are encountered and the passed in exception is null, it completes normally.
        Parameters:
        objects - objects to close
        Throws:
        IOException
      • close

        public static void close​(Iterable<? extends Closeable> objects)
                          throws IOException
        Closes all given Closeables. Some of the Closeables may be null; they are ignored. After everything is closed, the method either throws the first exception it hit while closing with other exceptions added as suppressed, or completes normally if there were no exceptions.
        Parameters:
        objects - objects to close
        Throws:
        IOException
      • closeWhileHandlingException

        public static void closeWhileHandlingException​(Closeable... objects)
        Closes all given Closeables, suppressing all thrown exceptions. Some of the Closeables may be null, they are ignored.
        Parameters:
        objects - objects to close
      • deleteFilesIgnoringExceptions

        public static void deleteFilesIgnoringExceptions​(Path... files)
        Deletes all given files, suppressing all thrown IOExceptions. Some of the files may be null, if so they are ignored.
        Parameters:
        files - the paths of files to delete
      • deleteFilesIgnoringExceptions

        public static void deleteFilesIgnoringExceptions​(Collection<? extends Path> files)
        Deletes all given files, suppressing all thrown IOExceptions. Some of the files may be null, if so they are ignored.
        Parameters:
        files - the paths of files to delete
      • rm

        public static void rm​(Path... locations)
                       throws IOException
        Deletes one or more files or directories (and everything underneath it).
        Throws:
        IOException - if any of the given files (or their sub-hierarchy files in case of directories) cannot be removed.
      • fsync

        public static void fsync​(Path fileToSync,
                                 boolean isDir)
                          throws IOException
        Ensure that any writes to the given file is written to the storage device that contains it. The isDir parameter specifies whether or not the path to sync is a directory. This is needed because we open for read and ignore an IOException since not all filesystems and operating systems support fsyncing on a directory. For regular files we must open for write for the fsync to have an effect.
        Parameters:
        fileToSync - the file to fsync
        isDir - if true, the given file is a directory (we open for read and ignore IOExceptions, because not all file systems and operating systems allow to fsync on a directory)
        Throws:
        IOException