Class File

java.lang.Object
org.apache.cassandra.io.util.File
All Implemented Interfaces:
Comparable<File>

public class File extends Object implements 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
  • Constructor Details

    • File

      public File(String parent, String child)
      Construct a File representing the child child of parent
    • File

      public File(File parent, String child)
      Construct a File representing the child child of parent
    • File

      public File(Path parent, String child)
      Construct a File representing the child child of parent
    • File

      public File(String path)
      Construct a File representing the provided path
    • File

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

      public File(File parent, String child)
      Construct a File representing the child child of parent
    • File

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

      public File(FileSystem fs, String first, 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(Path path)
      Parameters:
      path - the path to wrap
  • Method Details

    • pathSeparator

      public static String pathSeparator()
    • getPath

      public static Path getPath(String first, 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 Throwable delete(Throwable accumulate)
      This file will be deleted, and any exceptions encountered merged with accumulate to the return value
    • delete

      public Throwable delete(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 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 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 String canonicalPath()
    • name

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

      public void forEach(Consumer<File> forEach)
    • forEachRecursive

      public void forEachRecursive(Consumer<File> forEach)
    • tryListNames

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

      public String[] tryListNames(BiPredicate<File,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(Predicate<File> filter)
      Returns:
      if a directory, the files within, filtered by the provided predicate; null otherwise
    • tryList

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • compareTo

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

      public File toJavaIOFile()
    • newReadChannel

      public FileChannel newReadChannel() throws NoSuchFileException
      Returns:
      a new FileChannel for reading
      Throws:
      NoSuchFileException
    • newReadWriteChannel

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

      public FileChannel newWriteChannel(File.WriteMode mode) throws 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:
      NoSuchFileException
    • newWriter

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

      public FileOutputStreamPlus newOutputStream(File.WriteMode mode) throws NoSuchFileException
      Throws:
      NoSuchFileException
    • newInputStream

      public FileInputStreamPlus newInputStream() throws NoSuchFileException
      Throws:
      NoSuchFileException
    • withSuffix

      public File withSuffix(String suffix)
    • unsafeGetFilesystem

      public static FileSystem unsafeGetFilesystem()
    • unsafeSetFilesystem

      public static void unsafeSetFilesystem(FileSystem fs)