Package org.apache.cassandra.io.util
Class File
- java.lang.Object
-
- org.apache.cassandra.io.util.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 classFile.WriteMode
-
Constructor Summary
Constructors Constructor Description File(java.io.File file)Create a File equivalent to the java.io.File providedFile(java.io.File parent, java.lang.String child)Construct a File representing the childchildofparentFile(java.lang.String path)Construct a File representing the providedpathFile(java.lang.String parent, java.lang.String child)Construct a File representing the childchildofparentFile(java.net.URI path)Convenience constructor equivalent tonew File(Paths.get(path))File(java.nio.file.Path path)File(java.nio.file.Path parent, java.lang.String child)Construct a File representing the childchildofparentFile(File parent, java.lang.String child)Construct a File representing the childchildofparent
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.StringabsolutePath()java.lang.StringcanonicalPath()intcompareTo(File that)booleancreateDirectoriesIfNotExists()booleancreateFileIfNotExists()Try to create a new regular file at this path.voiddelete()This file will be deleted, with any failures being reported with an FSErrorvoiddelete(com.google.common.util.concurrent.RateLimiter rateLimiter)This file will be deleted, obeying the provided rate limiter.java.lang.Throwabledelete(java.lang.Throwable accumulate)This file will be deleted, and any exceptions encountered merged withaccumulateto the return valuejava.lang.Throwabledelete(java.lang.Throwable accumulate, com.google.common.util.concurrent.RateLimiter rateLimiter)This file will be deleted, obeying the provided rate limiter.voiddeleteIfExists()This file will be deleted, with any failures being reported with an FSErrorvoiddeleteOnExit()Try to delete the file on process exit.voiddeleteRecursive()Deletes all files and subdirectories under "dir".voiddeleteRecursive(com.google.common.util.concurrent.RateLimiter rateLimiter)Deletes all files and subdirectories under "dir".voiddeleteRecursiveOnExit()This file will be deleted on clean shutdown; if it is a directory, its entire contents at the time of shutdown will be deletedbooleanequals(java.lang.Object obj)booleanexists()voidforEach(java.util.function.Consumer<File> forEach)voidforEachRecursive(java.util.function.Consumer<File> forEach)static java.nio.file.PathgetPath(java.lang.String first, java.lang.String... more)inthashCode()booleanisAbsolute()booleanisAncestorOf(File child)booleanisDirectory()booleanisExecutable()booleanisFile()booleanisReadable()booleanisWritable()longlastModified()longlength()File[]list()File[]list(java.util.function.BiPredicate<File,java.lang.String> filter)File[]list(java.util.function.Predicate<File> filter)java.lang.String[]listNames()java.lang.String[]listNames(java.util.function.BiPredicate<File,java.lang.String> filter)java.lang.String[]listNamesUnchecked()java.lang.String[]listNamesUnchecked(java.util.function.BiPredicate<File,java.lang.String> filter)File[]listUnchecked()File[]listUnchecked(java.util.function.BiPredicate<File,java.lang.String> filter)File[]listUnchecked(java.util.function.Predicate<File> filter)voidmove(File to)Atomically (if supported) rename/move this file totojava.lang.Stringname()FileInputStreamPlusnewInputStream()FileOutputStreamPlusnewOutputStream(File.WriteMode mode)java.nio.channels.FileChannelnewReadChannel()java.nio.channels.FileChannelnewReadWriteChannel()java.nio.channels.FileChannelnewWriteChannel(File.WriteMode mode)FileWriternewWriter(File.WriteMode mode)Fileparent()java.lang.StringparentPath()java.lang.Stringpath()static java.lang.StringpathSeparator()FiletoAbsolute()FiletoCanonical()java.io.FiletoJavaIOFile()java.nio.file.PathtoPath()java.lang.StringtoString()booleantryCreateDirectories()Try to create a directory at this path, creating any parent directories as necessary.booleantryCreateDirectory()Try to create a directory at this path.booleantryDelete()Try to delete the file, returning true iff it was deleted by us.File[]tryList()File[]tryList(java.util.function.BiPredicate<File,java.lang.String> filter)File[]tryList(java.util.function.Predicate<File> filter)java.lang.String[]tryListNames()java.lang.String[]tryListNames(java.util.function.BiPredicate<File,java.lang.String> filter)<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)<T extends java.lang.Throwable>
java.lang.String[]tryListNames(net.openhft.chronicle.core.util.ThrowingFunction<java.io.IOException,java.lang.String[],T> orElse)booleantryMove(File to)Try to rename the file atomically, if the system supports it.booleantrySetExecutable(boolean value)Try to set if the path is executable by its ownerbooleantrySetLastModified(long value)Try to set the last modified time in millis of the pathbooleantrySetReadable(boolean value)Try to set if the path is readable by its ownerbooleantrySetWritable(boolean value)Try to set if the path is writable by its ownerstatic voidunsafeSetFilesystem(java.nio.file.FileSystem fs)
-
-
-
Constructor Detail
-
File
public File(java.lang.String parent, java.lang.String child)Construct a File representing the childchildofparent
-
File
public File(File parent, java.lang.String child)
Construct a File representing the childchildofparent
-
File
public File(java.nio.file.Path parent, java.lang.String child)Construct a File representing the childchildofparent
-
File
public File(java.lang.String path)
Construct a File representing the providedpath
-
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 childchildofparent
-
File
public File(java.net.URI path)
Convenience constructor equivalent tonew File(Paths.get(path))
-
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 withaccumulateto 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 withaccumulateto 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 toto- 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
Pathof this file
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object- Returns:
- the path of this file
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
equals
public boolean equals(java.lang.Object obj)
- Overrides:
equalsin classjava.lang.Object
-
compareTo
public int compareTo(File that)
- Specified by:
compareToin interfacejava.lang.Comparable<File>
-
toJavaIOFile
public java.io.File toJavaIOFile()
-
newReadChannel
public java.nio.channels.FileChannel newReadChannel() throws java.nio.file.NoSuchFileException- Returns:
- a new
FileChannelfor reading - Throws:
java.nio.file.NoSuchFileException
-
newReadWriteChannel
public java.nio.channels.FileChannel newReadWriteChannel() throws java.nio.file.NoSuchFileException- Returns:
- a new
FileChannelfor 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
FileChannelfor 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
-
unsafeSetFilesystem
public static void unsafeSetFilesystem(java.nio.file.FileSystem fs)
-
-