Class FileUtils


  • public final class FileUtils
    extends java.lang.Object
    • Method Detail

      • getTempDir

        public static File getTempDir()
      • createTempFile

        public static File createTempFile​(java.lang.String prefix,
                                          java.lang.String suffix,
                                          File directory)
        Pretty much like File.createTempFile(String, String, java.io.File), but with the guarantee that the "random" part of the generated file name between prefix and suffix is a positive, increasing long value.
      • createTempFile

        public static File createTempFile​(java.lang.String prefix,
                                          java.lang.String suffix)
      • createDeletableTempFile

        public static File createDeletableTempFile​(java.lang.String prefix,
                                                   java.lang.String suffix)
      • createHardLink

        public static void createHardLink​(java.lang.String from,
                                          java.lang.String to)
      • createHardLink

        public static void createHardLink​(File from,
                                          File to)
      • createHardLinkWithConfirm

        public static void createHardLinkWithConfirm​(java.lang.String from,
                                                     java.lang.String to)
      • createHardLinkWithConfirm

        public static void createHardLinkWithConfirm​(File from,
                                                     File to)
      • createHardLinkWithoutConfirm

        public static void createHardLinkWithoutConfirm​(java.lang.String from,
                                                        java.lang.String to)
      • createHardLinkWithoutConfirm

        public static void createHardLinkWithoutConfirm​(File from,
                                                        File to)
      • copyWithOutConfirm

        public static void copyWithOutConfirm​(java.lang.String from,
                                              java.lang.String to)
      • copyWithOutConfirm

        public static void copyWithOutConfirm​(File from,
                                              File to)
      • copyWithConfirm

        public static void copyWithConfirm​(java.lang.String from,
                                           java.lang.String to)
      • copyWithConfirm

        public static void copyWithConfirm​(File from,
                                           File to)
      • truncate

        public static void truncate​(java.lang.String path,
                                    long size)
      • closeQuietly

        public static void closeQuietly​(java.io.Closeable c)
      • closeQuietly

        public static void closeQuietly​(java.lang.AutoCloseable c)
      • close

        public static void close​(java.io.Closeable... cs)
                          throws java.io.IOException
        Throws:
        java.io.IOException
      • close

        public static void close​(java.lang.Iterable<? extends java.io.Closeable> cs)
                          throws java.io.IOException
        Throws:
        java.io.IOException
      • closeQuietly

        public static void closeQuietly​(java.lang.Iterable<? extends java.lang.AutoCloseable> cs)
      • getCanonicalPath

        public static java.lang.String getCanonicalPath​(java.lang.String filename)
      • getCanonicalPath

        public static java.lang.String getCanonicalPath​(File file)
      • isContained

        public static boolean isContained​(File folder,
                                          File file)
        Return true if file is contained in folder
      • clean

        public static void clean​(java.nio.ByteBuffer buffer)
      • parseFileSize

        public static long parseFileSize​(java.lang.String value)
      • stringifyFileSize

        public static java.lang.String stringifyFileSize​(double value)
      • handleFSError

        public static void handleFSError​(FSError e)
      • handleStartupFSError

        public static void handleStartupFSError​(java.lang.Throwable t)
      • handleFSErrorAndPropagate

        public static void handleFSErrorAndPropagate​(FSError e)
        handleFSErrorAndPropagate will invoke the disk failure policy error handler, which may or may not stop the daemon or transports. However, if we don't exit, we still want to propagate the exception to the caller in case they have custom exception handling
        Parameters:
        e - A filesystem error
      • folderSize

        public static long folderSize​(File folder)
        Get the size of a directory in bytes
        Parameters:
        folder - The directory for which we need size.
        Returns:
        The size of the directory
      • append

        public static void append​(File file,
                                  java.lang.String... lines)
      • appendAndSync

        public static void appendAndSync​(File file,
                                         java.lang.String... lines)
      • replace

        public static void replace​(File file,
                                   java.lang.String... lines)
      • write

        public static void write​(File file,
                                 java.util.List<java.lang.String> lines,
                                 java.nio.file.StandardOpenOption... options)
        Write lines to a file adding a newline to the end of each supplied line using the provided open options. If open option sync or dsync is provided this will not open the file with sync or dsync since it might end up syncing many times for a lot of lines. Instead it will write all the lines and sync once at the end. Since the file is never returned there is not much difference from the perspective of the caller.
        Parameters:
        file -
        lines -
        options -
      • readLines

        public static java.util.List<java.lang.String> readLines​(File file)
      • setFSErrorHandler

        public static void setFSErrorHandler​(FSErrorHandler handler)
      • createDirectory

        @Deprecated(since="4.1")
        public static void createDirectory​(java.lang.String directory)
        Deprecated.
        See CASSANDRA-16926
      • createDirectory

        @Deprecated(since="4.1")
        public static void createDirectory​(File directory)
        Deprecated.
        See CASSANDRA-16926
      • delete

        @Deprecated(since="4.1")
        public static boolean delete​(java.lang.String file)
        Deprecated.
        See CASSANDRA-16926
      • delete

        @Deprecated(since="4.1")
        public static void delete​(File... files)
        Deprecated.
        See CASSANDRA-16926
      • deleteRecursiveWithThrottle

        @Deprecated(since="4.1")
        public static void deleteRecursiveWithThrottle​(File dir,
                                                       com.google.common.util.concurrent.RateLimiter rateLimiter)
        Deprecated.
        See CASSANDRA-16926
        Deletes all files and subdirectories under "dir".
        Parameters:
        dir - Directory to be deleted
        Throws:
        FSWriteError - if any part of the tree cannot be deleted
      • deleteRecursive

        @Deprecated(since="4.1")
        public static void deleteRecursive​(File dir)
        Deprecated.
        See CASSANDRA-16926
        Deletes all files and subdirectories under "dir".
        Parameters:
        dir - Directory to be deleted
        Throws:
        FSWriteError - if any part of the tree cannot be deleted
      • deleteRecursiveOnExit

        @Deprecated(since="4.1")
        public static void deleteRecursiveOnExit​(File dir)
        Deprecated.
        See CASSANDRA-16926
        Schedules deletion of all file and subdirectories under "dir" on JVM shutdown.
        Parameters:
        dir - Directory to be deleted
      • isSubDirectory

        @Deprecated(since="4.1")
        public static boolean isSubDirectory​(File parent,
                                             File child)
        Deprecated.
        See CASSANDRA-16926
      • deleteWithConfirm

        @Deprecated(since="4.1")
        public static java.lang.Throwable deleteWithConfirm​(File file,
                                                            java.lang.Throwable accumulate)
        Deprecated.
        See CASSANDRA-16926
      • deleteWithConfirm

        @Deprecated(since="4.1")
        public static java.lang.Throwable deleteWithConfirm​(File file,
                                                            java.lang.Throwable accumulate,
                                                            com.google.common.util.concurrent.RateLimiter rateLimiter)
        Deprecated.
        See CASSANDRA-16926
      • deleteWithConfirm

        @Deprecated(since="4.1")
        public static void deleteWithConfirm​(java.lang.String file)
        Deprecated.
        See CASSANDRA-16926
      • deleteWithConfirm

        @Deprecated(since="4.1")
        public static void deleteWithConfirm​(File file)
        Deprecated.
        See CASSANDRA-16926
      • renameWithOutConfirm

        @Deprecated(since="4.1")
        public static void renameWithOutConfirm​(java.lang.String from,
                                                java.lang.String to)
        Deprecated.
        See CASSANDRA-16926
      • renameWithConfirm

        @Deprecated(since="4.1")
        public static void renameWithConfirm​(java.lang.String from,
                                             java.lang.String to)
        Deprecated.
        See CASSANDRA-16926
      • renameWithConfirm

        @Deprecated(since="4.1")
        public static void renameWithConfirm​(File from,
                                             File to)
        Deprecated.
        See CASSANDRA-16926
      • moveRecursively

        public static void moveRecursively​(java.nio.file.Path source,
                                           java.nio.file.Path target)
                                    throws java.io.IOException
        Moves the contents of a directory to another directory.

        Once a file has been copied to the target directory it will be deleted from the source directory. If a file already exists in the target directory a warning will be logged and the file will not be deleted.

        Parameters:
        source - the directory containing the files to move
        target - the directory where the files must be moved
        Throws:
        java.io.IOException
      • deleteDirectoryIfEmpty

        public static void deleteDirectoryIfEmpty​(java.nio.file.Path path)
                                           throws java.io.IOException
        Deletes the specified directory if it is empty
        Parameters:
        path - the path to the directory
        Throws:
        java.io.IOException
      • getBlockSize

        public static int getBlockSize​(File directory)