Class File

  • All Implemented Interfaces:
    java.lang.Comparable<File>

    public class File
    extends java.lang.Object
    implements java.lang.Comparable<File>
    A thin wrapper around java.nio.file.Path to provide more ergonomic functionality. TODO codebase probably should not use tryList, as unexpected exceptions are hidden; probably want to introduce e.g. listIfExists
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  File.WriteMode  
    • Constructor Summary

      Constructors 
      Constructor Description
      File​(java.io.File file)
      Create a File equivalent to the java.io.File provided
      File​(java.io.File parent, java.lang.String child)
      Construct a File representing the child child of parent
      File​(java.lang.String path)
      Construct a File representing the provided path
      File​(java.lang.String parent, java.lang.String child)
      Construct a File representing the child child of parent
      File​(java.net.URI path)
      Convenience constructor equivalent to new File(Paths.get(path))
      File​(java.nio.file.FileSystem fs, java.lang.String first, java.lang.String... more)
      Unsafe constructor that allows a File to use a differet FileSystem than filesystem.
      File​(java.nio.file.Path path)  
      File​(java.nio.file.Path parent, java.lang.String child)
      Construct a File representing the child child of parent
      File​(File parent, java.lang.String child)
      Construct a File representing the child child of parent
    • Constructor Detail

      • File

        public File​(java.lang.String parent,
                    java.lang.String child)
        Construct a File representing the child child of parent
      • File

        public File​(File parent,
                    java.lang.String child)
        Construct a File representing the child child of parent
      • File

        public File​(java.nio.file.Path parent,
                    java.lang.String child)
        Construct a File representing the child child of parent
      • File

        public File​(java.lang.String path)
        Construct a File representing the provided path
      • File

        public File​(java.io.File file)
        Create a File equivalent to the java.io.File provided
      • File

        public File​(java.io.File parent,
                    java.lang.String child)
        Construct a File representing the child child of parent
      • File

        public File​(java.net.URI path)
        Convenience constructor equivalent to new File(Paths.get(path))
      • File

        public File​(java.nio.file.FileSystem fs,
                    java.lang.String first,
                    java.lang.String... more)
        Unsafe constructor that allows a File to use a differet FileSystem than filesystem. The main caller of such a method are cases such as JVM Dtest functions that need access to the logging framwork files, which exists on in FileSystems.getDefault().
      • File

        public File​(java.nio.file.Path path)
        Parameters:
        path - the path to wrap
    • Method Detail

      • pathSeparator

        public static java.lang.String pathSeparator()
      • getPath

        public static java.nio.file.Path getPath​(java.lang.String first,
                                                 java.lang.String... more)
      • tryDelete

        public boolean tryDelete()
        Try to delete the file, returning true iff it was deleted by us. Does not ordinarily throw exceptions.
      • delete

        public java.lang.Throwable delete​(java.lang.Throwable accumulate)
        This file will be deleted, and any exceptions encountered merged with accumulate to the return value
      • delete

        public java.lang.Throwable delete​(java.lang.Throwable accumulate,
                                          com.google.common.util.concurrent.RateLimiter rateLimiter)
        This file will be deleted, obeying the provided rate limiter. Any exceptions encountered will be merged with accumulate to the return value
      • delete

        public void delete()
        This file will be deleted, with any failures being reported with an FSError
        Throws:
        FSWriteError - if cannot be deleted
      • deleteIfExists

        public void deleteIfExists()
        This file will be deleted, with any failures being reported with an FSError
        Throws:
        FSWriteError - if cannot be deleted
      • delete

        public void delete​(com.google.common.util.concurrent.RateLimiter rateLimiter)
        This file will be deleted, obeying the provided rate limiter.
        Throws:
        FSWriteError - if cannot be deleted
      • deleteRecursive

        public void deleteRecursive​(com.google.common.util.concurrent.RateLimiter rateLimiter)
        Deletes all files and subdirectories under "dir".
        Throws:
        FSWriteError - if any part of the tree cannot be deleted
      • deleteRecursive

        public void deleteRecursive()
        Deletes all files and subdirectories under "dir".
        Throws:
        FSWriteError - if any part of the tree cannot be deleted
      • deleteOnExit

        public void deleteOnExit()
        Try to delete the file on process exit.
      • deleteRecursiveOnExit

        public void deleteRecursiveOnExit()
        This file will be deleted on clean shutdown; if it is a directory, its entire contents at the time of shutdown will be deleted
      • tryMove

        public boolean tryMove​(File to)
        Try to rename the file atomically, if the system supports it.
        Returns:
        true iff successful, false if it fails for any reason.
      • move

        public void move​(File to)
        Atomically (if supported) rename/move this file to to
        Throws:
        FSWriteError - if any part of the tree cannot be deleted
      • length

        public long length()
        Returns:
        the length of the file if it exists and if we can read it; 0 otherwise.
      • lastModified

        public long lastModified()
        Returns:
        the last modified time in millis of the path if it exists and we can read it; 0 otherwise.
      • trySetLastModified

        public boolean trySetLastModified​(long value)
        Try to set the last modified time in millis of the path
        Returns:
        true if it exists and we can write it; return false otherwise.
      • trySetReadable

        public boolean trySetReadable​(boolean value)
        Try to set if the path is readable by its owner
        Returns:
        true if it exists and we can write it; return false otherwise.
      • trySetWritable

        public boolean trySetWritable​(boolean value)
        Try to set if the path is writable by its owner
        Returns:
        true if it exists and we can write it; return false otherwise.
      • trySetExecutable

        public boolean trySetExecutable​(boolean value)
        Try to set if the path is executable by its owner
        Returns:
        true if it exists and we can write it; return false otherwise.
      • exists

        public boolean exists()
        Returns:
        true if the path exists, false if it does not, or we cannot determine due to some exception
      • isDirectory

        public boolean isDirectory()
        Returns:
        true if the path refers to a directory
      • isFile

        public boolean isFile()
        Returns:
        true if the path refers to a regular file
      • isReadable

        public boolean isReadable()
        Returns:
        true if the path can be read by us
      • isWritable

        public boolean isWritable()
        Returns:
        true if the path can be written by us
      • isExecutable

        public boolean isExecutable()
        Returns:
        true if the path can be executed by us
      • createFileIfNotExists

        public boolean createFileIfNotExists()
        Try to create a new regular file at this path.
        Returns:
        true if successful, false if it already exists
      • createDirectoriesIfNotExists

        public boolean createDirectoriesIfNotExists()
      • tryCreateDirectory

        public boolean tryCreateDirectory()
        Try to create a directory at this path. Return true if a new directory was created at this path, and false otherwise.
      • tryCreateDirectories

        public boolean tryCreateDirectories()
        Try to create a directory at this path, creating any parent directories as necessary.
        Returns:
        true if a new directory was created at this path, and false otherwise.
      • parent

        public File parent()
        Returns:
        the parent file, or null if none
      • parentPath

        public java.lang.String parentPath()
        Returns:
        the parent file's path, or null if none
      • isAbsolute

        public boolean isAbsolute()
        Returns:
        true if the path has no relative path elements
      • isAncestorOf

        public boolean isAncestorOf​(File child)
      • toAbsolute

        public File toAbsolute()
        Returns:
        a File that represents the same path as this File with any relative path elements resolved. If this is the empty File, returns the working directory.
      • absolutePath

        public java.lang.String absolutePath()
      • toCanonical

        public File toCanonical()
        Returns:
        a File that represents the same path as this File with any relative path elements and links resolved. If this is the empty File, returns the working directory.
      • canonicalPath

        public java.lang.String canonicalPath()
      • name

        public java.lang.String name()
        Returns:
        the last path element for this file
      • forEach

        public void forEach​(java.util.function.Consumer<File> forEach)
      • forEachRecursive

        public void forEachRecursive​(java.util.function.Consumer<File> forEach)
      • tryListNames

        public java.lang.String[] tryListNames()
        Returns:
        if a directory, the names of the files within; null otherwise
      • tryListNames

        public java.lang.String[] tryListNames​(java.util.function.BiPredicate<File,​java.lang.String> filter)
        Returns:
        if a directory, the names of the files within, filtered by the provided predicate; null otherwise
      • tryList

        public File[] tryList()
        Returns:
        if a directory, the files within; null otherwise
      • tryList

        public File[] tryList​(java.util.function.Predicate<File> filter)
        Returns:
        if a directory, the files within, filtered by the provided predicate; null otherwise
      • tryList

        public File[] tryList​(java.util.function.BiPredicate<File,​java.lang.String> filter)
        Returns:
        if a directory, the files within, filtered by the provided predicate; null otherwise
      • listNames

        public java.lang.String[] listNames()
                                     throws java.io.IOException
        Returns:
        if a directory, the names of the files within; null otherwise
        Throws:
        java.io.IOException
      • listNames

        public java.lang.String[] listNames​(java.util.function.BiPredicate<File,​java.lang.String> filter)
                                     throws java.io.IOException
        Returns:
        if a directory, the names of the files within, filtered by the provided predicate; null otherwise
        Throws:
        java.io.IOException
      • list

        public File[] list()
                    throws java.io.IOException
        Returns:
        if a directory, the files within; null otherwise
        Throws:
        java.io.IOException
      • list

        public File[] list​(java.util.function.Predicate<File> filter)
                    throws java.io.IOException
        Returns:
        if a directory, the files within, filtered by the provided predicate; null otherwise
        Throws:
        java.io.IOException
      • list

        public File[] list​(java.util.function.BiPredicate<File,​java.lang.String> filter)
                    throws java.io.IOException
        Returns:
        if a directory, the files within, filtered by the provided predicate; null otherwise
        Throws:
        java.io.IOException
      • listNamesUnchecked

        public java.lang.String[] listNamesUnchecked()
                                              throws java.io.UncheckedIOException
        Returns:
        if a directory, the names of the files within; null otherwise
        Throws:
        java.io.UncheckedIOException
      • listNamesUnchecked

        public java.lang.String[] listNamesUnchecked​(java.util.function.BiPredicate<File,​java.lang.String> filter)
                                              throws java.io.UncheckedIOException
        Returns:
        if a directory, the names of the files within, filtered by the provided predicate; null otherwise
        Throws:
        java.io.UncheckedIOException
      • listUnchecked

        public File[] listUnchecked()
                             throws java.io.UncheckedIOException
        Returns:
        if a directory, the files within; null otherwise
        Throws:
        java.io.UncheckedIOException
      • listUnchecked

        public File[] listUnchecked​(java.util.function.Predicate<File> filter)
                             throws java.io.UncheckedIOException
        Returns:
        if a directory, the files within, filtered by the provided predicate; null otherwise
        Throws:
        java.io.UncheckedIOException
      • listUnchecked

        public File[] listUnchecked​(java.util.function.BiPredicate<File,​java.lang.String> filter)
                             throws java.io.UncheckedIOException
        Returns:
        if a directory, the files within, filtered by the provided predicate; throw an UncheckedIO exception otherwise
        Throws:
        java.io.UncheckedIOException
      • tryListNames

        public <T extends java.lang.Throwable> java.lang.String[] tryListNames​(net.openhft.chronicle.core.util.ThrowingFunction<java.io.IOException,​java.lang.String[],​T> orElse)
                                                                        throws T extends java.lang.Throwable
        Returns:
        if a directory, the names of the files within; null otherwise
        Throws:
        T extends java.lang.Throwable
      • tryListNames

        public <T extends java.lang.Throwable> java.lang.String[] tryListNames​(java.util.function.BiPredicate<File,​java.lang.String> filter,
                                                                               net.openhft.chronicle.core.util.ThrowingFunction<java.io.IOException,​java.lang.String[],​T> orElse)
                                                                        throws T extends java.lang.Throwable
        Returns:
        if a directory, the names of the files within, filtered by the provided predicate; null otherwise
        Throws:
        T extends java.lang.Throwable
      • path

        public java.lang.String path()
        Returns:
        the path of this file
      • toPath

        public java.nio.file.Path toPath()
        Returns:
        the Path of this file
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
        Returns:
        the path of this file
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • equals

        public boolean equals​(java.lang.Object obj)
        Overrides:
        equals in class java.lang.Object
      • compareTo

        public int compareTo​(File that)
        Specified by:
        compareTo in interface java.lang.Comparable<File>
      • toJavaIOFile

        public java.io.File toJavaIOFile()
      • newReadChannel

        public java.nio.channels.FileChannel newReadChannel()
                                                     throws java.nio.file.NoSuchFileException
        Returns:
        a new FileChannel for reading
        Throws:
        java.nio.file.NoSuchFileException
      • newReadWriteChannel

        public java.nio.channels.FileChannel newReadWriteChannel()
                                                          throws java.nio.file.NoSuchFileException
        Returns:
        a new FileChannel for reading or writing; file will be created if it doesn't exist
        Throws:
        java.nio.file.NoSuchFileException
      • newWriteChannel

        public java.nio.channels.FileChannel newWriteChannel​(File.WriteMode mode)
                                                      throws java.nio.file.NoSuchFileException
        Parameters:
        mode - whether or not the channel appends to the underlying file
        Returns:
        a new FileChannel for writing; file will be created if it doesn't exist
        Throws:
        java.nio.file.NoSuchFileException
      • newWriter

        public FileWriter newWriter​(File.WriteMode mode)
                             throws java.io.IOException
        Throws:
        java.io.IOException
      • newOutputStream

        public FileOutputStreamPlus newOutputStream​(File.WriteMode mode)
                                             throws java.nio.file.NoSuchFileException
        Throws:
        java.nio.file.NoSuchFileException
      • newInputStream

        public FileInputStreamPlus newInputStream()
                                           throws java.nio.file.NoSuchFileException
        Throws:
        java.nio.file.NoSuchFileException
      • withSuffix

        public File withSuffix​(java.lang.String suffix)
      • unsafeGetFilesystem

        public static java.nio.file.FileSystem unsafeGetFilesystem()
      • unsafeSetFilesystem

        public static void unsafeSetFilesystem​(java.nio.file.FileSystem fs)