class File extends AnyRef
Scala wrapper around java.nio.files.Path
- Alphabetic
- By Inheritance
- File
- AnyRef
- Any
- Hide All
- Show All
- Public
- All
Value Members
-
final
def
!=(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
final
def
##(): Int
- Definition Classes
- AnyRef → Any
- def /(child: Symbol): File
- def /(child: String): File
-
final
def
==(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- def addPermission(permission: PosixFilePermission)(implicit linkOptions: LinkOptions = File.LinkOptions.default): File.this.type
- def append(text: String)(implicit charset: Charset = defaultCharset): File.this.type
- def appendByteArray(bytes: Array[Byte]): File.this.type
- def appendBytes(bytes: Iterator[Byte]): File.this.type
- def appendLine(line: String = "")(implicit charset: Charset = defaultCharset): File.this.type
-
def
appendLines(lines: String*)(implicit charset: Charset = defaultCharset): File.this.type
For large number of lines that may not fit in memory, use printLines
- def appendText(text: String)(implicit charset: Charset = defaultCharset): File.this.type
-
final
def
asInstanceOf[T0]: T0
- Definition Classes
- Any
- def asynchronousFileChannel(implicit openOptions: OpenOptions = File.OpenOptions.default): ManagedResource[AsynchronousFileChannel]
- def attributes(implicit linkOptions: LinkOptions = File.LinkOptions.default): BasicFileAttributes
- def bufferedReader(implicit charset: Charset = defaultCharset): ManagedResource[BufferedReader]
- def bufferedWriter(implicit charset: Charset = defaultCharset, openOptions: OpenOptions = File.OpenOptions.default): ManagedResource[BufferedWriter]
- def byteArray: Array[Byte]
- def bytes: Iterator[Byte]
-
def
changeExtensionTo(extension: String): File
Changes the file-extension by renaming this file; if file does not have an extension, it adds the extension Example usage file"foo.java".changeExtensionTo(".scala")
- def chars(implicit charset: Charset = defaultCharset): Iterator[Char]
-
def
checksum(algorithm: MessageDigest): String
- returns
checksum of this file (or directory) in hex format
- def children: Files
-
def
clear()(implicit linkOptions: LinkOptions = File.LinkOptions.default): File.this.type
If this is a directory, remove all its children If its a file, empty the contents
If this is a directory, remove all its children If its a file, empty the contents
- returns
this
-
def
clone(): AnyRef
- Attributes
- protected[java.lang]
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
def
collectChildren(matchFilter: (File) ⇒ Boolean)(implicit visitOptions: VisitOptions = File.VisitOptions.default): Files
More Scala friendly way of doing Files.walk Note: This is lazy (returns an Iterator) and won't evaluate till we reify the iterator (e.g.
More Scala friendly way of doing Files.walk Note: This is lazy (returns an Iterator) and won't evaluate till we reify the iterator (e.g. using .toList)
-
def
contains(file: File): Boolean
Check if this directory contains this file
Check if this directory contains this file
- returns
true if this is a directory and it contains this file
- def contentAsString(implicit charset: Charset = defaultCharset): String
- def contentType: Option[String]
-
def
copyTo(destination: File, overwrite: Boolean = false)(implicit copyOptions: CopyOptions = File.CopyOptions(overwrite)): destination.type
- returns
destination
- def createChild(child: String, asDirectory: Boolean = false, createParents: Boolean = false)(implicit attributes: Attributes = File.Attributes.default, linkOptions: LinkOptions = File.LinkOptions.default): File
- def createDirectories()(implicit attributes: Attributes = File.Attributes.default): File.this.type
- def createDirectory()(implicit attributes: Attributes = File.Attributes.default): File.this.type
-
def
createIfNotExists(asDirectory: Boolean = false, createParents: Boolean = false)(implicit attributes: Attributes = File.Attributes.default, linkOptions: LinkOptions = File.LinkOptions.default): File.this.type
Create this file.
Create this file. If it exists, don't do anything
- asDirectory
If you want this file to be created as a directory instead, set this to true (false by default)
- createParents
If you also want all the parents to be created from root to this file (false by defailt)
-
def
delete(swallowIOExceptions: Boolean = false): File.this.type
Deletes this file or directory
Deletes this file or directory
- swallowIOExceptions
If this is set to true, any exception thrown is swallowed
- def deleteOnExit(): File.this.type
- def digest(algorithm: MessageDigest): Array[Byte]
- def digestInputStream(digest: MessageDigest)(implicit openOptions: OpenOptions = File.OpenOptions.default): ManagedResource[DigestInputStream]
- def dosAttributes(implicit linkOptions: LinkOptions = File.LinkOptions.default): DosFileAttributes
- def entries: Files
-
final
def
eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
-
def
equals(obj: Any): Boolean
- Definition Classes
- File → AnyRef → Any
- def exists(implicit linkOptions: LinkOptions = File.LinkOptions.default): Boolean
-
def
extension(includeDot: Boolean = true, includeAll: Boolean = false, toLowerCase: Boolean = true): Option[String]
- includeDot
whether the dot should be included in the extension or not
- includeAll
whether all extension tokens should be included, or just the last one e.g. for bundle.tar.gz should it be .tar.gz or .gz
- toLowerCase
to lowercase the extension or not e.g. foo.HTML should have .html or .HTML
- returns
extension of this file if it is a regular file and has an extension, else None
-
def
extension: Option[String]
- returns
extension (including the dot) of this file if it is a regular file and has an extension, else None
- def fileChannel(implicit openOptions: OpenOptions = File.OpenOptions.default, attributes: Attributes = File.Attributes.default): ManagedResource[FileChannel]
- def fileReader: ManagedResource[FileReader]
- implicit val fileSystem: FileSystem
- def fileWriter(append: Boolean = false): ManagedResource[FileWriter]
-
def
finalize(): Unit
- Attributes
- protected[java.lang]
- Definition Classes
- AnyRef
- Annotations
- @throws( classOf[java.lang.Throwable] )
-
final
def
getClass(): Class[_]
- Definition Classes
- AnyRef → Any
-
def
glob(pattern: String, includePath: Boolean = true)(implicit syntax: PathMatcherSyntax = File.PathMatcherSyntax.default, visitOptions: VisitOptions = File.VisitOptions.default): Files
Util to glob from this file's path
Util to glob from this file's path
- includePath
If true, we don't need to set path glob patterns e.g. instead of *
-
def
globRegex(pattern: Regex, includePath: Boolean = true)(implicit visitOptions: VisitOptions = File.VisitOptions.default): Files
Util to match from this file's path using Regex
Util to match from this file's path using Regex
- includePath
If true, we don't need to set path glob patterns e.g. instead of *
- def group(implicit linkOptions: LinkOptions = File.LinkOptions.default): GroupPrincipal
- def groupName(implicit linkOptions: LinkOptions = File.LinkOptions.default): String
-
def
hasExtension: Boolean
Returns the extension if file is a regular file If file is unreadable or does not exist, it is assumed to be not a regular file See: https://github.com/pathikrit/better-files/issues/89
-
def
hashCode(): Int
- Definition Classes
- File → AnyRef → Any
- def inputStream(implicit openOptions: OpenOptions = File.OpenOptions.default): ManagedResource[InputStream]
- def isChildOf(parent: File): Boolean
-
def
isDirectory(implicit linkOptions: LinkOptions = File.LinkOptions.default): Boolean
- returns
true if this file (or the file found by following symlink) is a directory
-
def
isEmpty(implicit linkOptions: LinkOptions = File.LinkOptions.default): Boolean
- returns
true if file is not present or empty directory or 0-bytes file
- def isExecutable: Boolean
- def isGroupExecutable(implicit linkOptions: LinkOptions = File.LinkOptions.default): Boolean
- def isGroupReadable(implicit linkOptions: LinkOptions = File.LinkOptions.default): Boolean
- def isGroupWritable(implicit linkOptions: LinkOptions = File.LinkOptions.default): Boolean
- def isHidden: Boolean
-
final
def
isInstanceOf[T0]: Boolean
- Definition Classes
- Any
- def isLocked(mode: RandomAccessMode, position: Long = 0L, size: Long = Long.MaxValue, isShared: Boolean = false): Boolean
- def isOtherExecutable(implicit linkOptions: LinkOptions = File.LinkOptions.default): Boolean
- def isOtherReadable(implicit linkOptions: LinkOptions = File.LinkOptions.default): Boolean
- def isOtherWritable(implicit linkOptions: LinkOptions = File.LinkOptions.default): Boolean
- def isOwnerExecutable(implicit linkOptions: LinkOptions = File.LinkOptions.default): Boolean
- def isOwnerReadable(implicit linkOptions: LinkOptions = File.LinkOptions.default): Boolean
- def isOwnerWritable(implicit linkOptions: LinkOptions = File.LinkOptions.default): Boolean
- def isParentOf(child: File): Boolean
- def isReadLocked(position: Long = 0L, size: Long = Long.MaxValue, isShared: Boolean = false): Boolean
-
def
isReadable: Boolean
This differs from the above as this checks if the JVM can read this file even though the OS cannot in certain platforms
This differs from the above as this checks if the JVM can read this file even though the OS cannot in certain platforms
- See also
isOwnerReadable
-
def
isRegularFile(implicit linkOptions: LinkOptions = File.LinkOptions.default): Boolean
- returns
true if this file (or the file found by following symlink) is a regular file
-
def
isSameContentAs(that: File): Boolean
- returns
true if this file is exactly same as that file For directories, it checks for equivalent directory structure
- def isSameFileAs(that: File): Boolean
- def isSamePathAs(that: File): Boolean
- def isSiblingOf(sibling: File): Boolean
-
def
isSimilarContentAs(that: File): Boolean
Almost same as isSameContentAs but uses faster md5 hashing to compare (and thus small chance of false positive) Also works for directories
- def isSymbolicLink: Boolean
- def isWriteLocked(position: Long = 0L, size: Long = Long.MaxValue, isShared: Boolean = false): Boolean
- def isWriteable: Boolean
- def lastModifiedTime(implicit linkOptions: LinkOptions = File.LinkOptions.default): Instant
-
def
lineIterator(implicit charset: Charset = defaultCharset): Iterator[String]
Iterate over lines in a file (auto-close stream on complete) NOTE: If the iteration is partial, it may leave a stream open If you want partial iteration use @see lines()
-
def
lines(implicit charset: Charset = defaultCharset): Traversable[String]
Load all lines from this file Note: Large files may cause an OutOfMemory in which case, use the streaming version @see lineIterator
Load all lines from this file Note: Large files may cause an OutOfMemory in which case, use the streaming version @see lineIterator
- returns
all lines in this file
- def linkTo(destination: File, symbolic: Boolean = false)(implicit attributes: Attributes = File.Attributes.default): destination.type
- def list: Files
- def listRecursively(implicit visitOptions: VisitOptions = File.VisitOptions.default): Files
- def listRelativePaths(implicit visitOptions: VisitOptions = File.VisitOptions.default): Iterator[Path]
- def loadBytes: Array[Byte]
- def md5: String
-
def
moveTo(destination: File, overwrite: Boolean = false): destination.type
- returns
destination
-
def
name: String
Name of file Certain files may not have a name e.g.
Name of file Certain files may not have a name e.g. root directory - returns empty string in that case
-
def
nameOption: Option[String]
Certain files may not have a name e.g.
Certain files may not have a name e.g. root directory - returns None in that case
-
def
nameWithoutExtension(includeAll: Boolean): String
- includeAll
For files with multiple extensions e.g. "bundle.tar.gz" nameWithoutExtension(includeAll = true) returns "bundle" nameWithoutExtension(includeAll = false) returns "bundle.tar"
- def nameWithoutExtension: String
-
final
def
ne(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- def newAsynchronousFileChannel(implicit openOptions: OpenOptions = File.OpenOptions.default): AsynchronousFileChannel
- def newBufferedReader(implicit charset: Charset = defaultCharset): BufferedReader
- def newBufferedWriter(implicit charset: Charset = defaultCharset, openOptions: OpenOptions = File.OpenOptions.default): BufferedWriter
- def newDigestInputStream(digest: MessageDigest)(implicit openOptions: OpenOptions = File.OpenOptions.default): DigestInputStream
- def newFileChannel(implicit openOptions: OpenOptions = File.OpenOptions.default, attributes: Attributes = File.Attributes.default): FileChannel
- def newFileReader: FileReader
- def newFileWriter(append: Boolean = false): FileWriter
- def newInputStream(implicit openOptions: OpenOptions = File.OpenOptions.default): InputStream
- def newOutputStream(implicit openOptions: OpenOptions = File.OpenOptions.default): OutputStream
- def newPrintWriter(autoFlush: Boolean = false)(implicit openOptions: OpenOptions = File.OpenOptions.default): PrintWriter
- def newRandomAccess(mode: RandomAccessMode = File.RandomAccessMode.read): RandomAccessFile
- def newScanner(implicit config: Config = Scanner.Config.default): Scanner
- def newWatchService: WatchService
- def newZipInputStream(implicit charset: Charset = defaultCharset): ZipInputStream
- def newZipOutputStream(implicit openOptions: OpenOptions = File.OpenOptions.default, charset: Charset = defaultCharset): ZipOutputStream
-
def
nonEmpty(implicit linkOptions: LinkOptions = File.LinkOptions.default): Boolean
- returns
for directories, true if it has no children, false otherwise for files, true if it is a 0-byte file, false otherwise else true if it exists, false otherwise
- def notExists(implicit linkOptions: LinkOptions = File.LinkOptions.default): Boolean
-
final
def
notify(): Unit
- Definition Classes
- AnyRef
-
final
def
notifyAll(): Unit
- Definition Classes
- AnyRef
- def outputStream(implicit openOptions: OpenOptions = File.OpenOptions.default): ManagedResource[OutputStream]
- def overwrite(text: String)(implicit openOptions: OpenOptions = File.OpenOptions.default, charset: Charset = defaultCharset): File.this.type
- def owner(implicit linkOptions: LinkOptions = File.LinkOptions.default): UserPrincipal
- def ownerName(implicit linkOptions: LinkOptions = File.LinkOptions.default): String
-
def
parent: File
Return parent of this file NOTE: This API returns null if this file is the root; please use parentOption if you expect to handle roots
Return parent of this file NOTE: This API returns null if this file is the root; please use parentOption if you expect to handle roots
- See also
parentOption
-
def
parentOption: Option[File]
- returns
Some(parent) of this file or None if this is the root and thus has no parent
- val path: Path
- def pathAsString: String
- def pathMatcher(syntax: PathMatcherSyntax, includePath: Boolean)(pattern: String): PathMatcher
- def permissions(implicit linkOptions: LinkOptions = File.LinkOptions.default): Set[PosixFilePermission]
- def permissionsAsString(implicit linkOptions: LinkOptions = File.LinkOptions.default): String
- def posixAttributes(implicit linkOptions: LinkOptions = File.LinkOptions.default): PosixFileAttributes
- def printLines(lines: Iterator[Any])(implicit openOptions: OpenOptions = File.OpenOptions.append): File.this.type
- def printWriter(autoFlush: Boolean = false)(implicit openOptions: OpenOptions = File.OpenOptions.default): ManagedResource[PrintWriter]
- def randomAccess(mode: RandomAccessMode = File.RandomAccessMode.read): ManagedResource[RandomAccessFile]
-
def
readDeserialized[A](implicit openOptions: OpenOptions = File.OpenOptions.default): A
Deserialize a object using Java's default serialization from this file
- def register(service: WatchService, events: Events = File.Events.all): File.this.type
- def relativize(destination: File): Path
- def removePermission(permission: PosixFilePermission)(implicit linkOptions: LinkOptions = File.LinkOptions.default): File.this.type
- def renameTo(newName: String): File
- def root: File
- def scanner(implicit config: Config = Scanner.Config.default): ManagedResource[Scanner]
- def setGroup(group: String): File.this.type
- def setOwner(owner: String): File.this.type
- def setPermissions(permissions: Set[PosixFilePermission]): File.this.type
- def sha1: String
- def sha256: String
- def sha512: String
- def sibling(name: String): File
- def siblings: Files
-
def
size(implicit visitOptions: VisitOptions = File.VisitOptions.default): Long
- returns
file size (for directories, return size of the directory) in bytes
-
def
streamedUnzip(destinationDirectory: File = File.newTemporaryDirectory(name))(implicit charset: Charset = defaultCharset): destinationDirectory.type
Streamed unzipping is slightly slower but supports larger files and more encodings
Streamed unzipping is slightly slower but supports larger files and more encodings
- destinationDirectory
destination folder; Creates this if it does not exist
- returns
The destination where contents are unzipped
- See also
https://github.com/pathikrit/better-files/issues/152
-
def
symbolicLink: Option[File]
- returns
Some(target) if this is a symbolic link (to target) else None
- def symbolicLinkTo(destination: File)(implicit attributes: Attributes = File.Attributes.default): destination.type
-
final
def
synchronized[T0](arg0: ⇒ T0): T0
- Definition Classes
- AnyRef
-
def
testPermission(permission: PosixFilePermission)(implicit linkOptions: LinkOptions = File.LinkOptions.default): Boolean
test if file has this permission
- def toJava: java.io.File
-
def
toString(): String
- Definition Classes
- File → AnyRef → Any
-
def
toTemporary: ManagedResource[File]
Java's temporary files/directories are not cleaned up by default.
Java's temporary files/directories are not cleaned up by default. If we explicitly call
.deleteOnExit()
, it gets added to shutdown handler which is not ideal for long running systems with millions of temporary files as: a) it would slowdown shutdown and b) occupy unnecessary disk-space during app lifetimeThis util auto-deletes the resource when done using the ManagedResource facility
Example usage: File.temporaryDirectory().foreach(tempDir => doSomething(tempDir)
- def tokens(implicit config: Config = Scanner.Config.default, charset: Charset = defaultCharset): Iterator[String]
-
def
touch(time: Instant = Instant.now())(implicit attributes: Attributes = File.Attributes.default, linkOptions: LinkOptions = File.LinkOptions.default): File.this.type
Similar to the UNIX command touch - create this file if it does not exist and set its last modification time
- def unGzipTo(destinationDirectory: File = File.newTemporaryDirectory())(implicit openOptions: OpenOptions = File.OpenOptions.default): destinationDirectory.type
-
def
unzip(zipFilter: (ZipEntry) ⇒ Boolean = _ => true)(implicit charset: Charset = defaultCharset): File
unzip to a temporary zip file
unzip to a temporary zip file
- returns
the zip file
-
def
unzipTo(destination: File, zipFilter: (ZipEntry) ⇒ Boolean = _ => true)(implicit charset: Charset = defaultCharset): destination.type
Unzips this zip file
Unzips this zip file
- destination
destination folder; Creates this if it does not exist
- zipFilter
An optional param to reject or accept unzipping a file
- returns
The destination where contents are unzipped
-
def
update(attribute: String, value: Any)(implicit linkOptions: LinkOptions = File.LinkOptions.default): File.this.type
Set a file attribute e.g.
Set a file attribute e.g. file("dos:system") = true
- def uri: URI
- def url: URL
- def usingLock[U](mode: RandomAccessMode)(f: (FileChannel) ⇒ U): U
-
final
def
wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
final
def
wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
final
def
wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
def
walk(maxDepth: Int = Int.MaxValue)(implicit visitOptions: VisitOptions = File.VisitOptions.default): Files
Walk the directory tree recursively upto maxDepth
Walk the directory tree recursively upto maxDepth
- returns
List of children in BFS maxDepth level deep (includes self since self is at depth = 0)
- def watchService: ManagedResource[WatchService]
- def write(text: String)(implicit openOptions: OpenOptions = File.OpenOptions.default, charset: Charset = defaultCharset): File.this.type
-
def
writeByteArray(bytes: Array[Byte])(implicit openOptions: OpenOptions = File.OpenOptions.default): File.this.type
Write byte array to file.
Write byte array to file. For large contents consider using the writeBytes
- returns
this
- def writeBytes(bytes: Iterator[Byte])(implicit openOptions: OpenOptions = File.OpenOptions.default): File.this.type
-
def
writeSerialized(obj: Serializable)(implicit openOptions: OpenOptions = File.OpenOptions.default): File.this.type
Serialize a object using Java's serializer into this file
- def writeText(text: String)(implicit openOptions: OpenOptions = File.OpenOptions.default, charset: Charset = defaultCharset): File.this.type
-
def
zip(compressionLevel: Int = Deflater.DEFAULT_COMPRESSION)(implicit charset: Charset = defaultCharset): File
zip to a temp directory
zip to a temp directory
- returns
the target directory
-
def
zipIn(files: Files, compressionLevel: Int = Deflater.DEFAULT_COMPRESSION)(charset: Charset = defaultCharset): File.this.type
Adds these files into this zip file Example usage: File("test.zip").zipIn(Seq(file"hello.txt", file"hello2.txt"))
Adds these files into this zip file Example usage: File("test.zip").zipIn(Seq(file"hello.txt", file"hello2.txt"))
- returns
this
- def zipInputStream(implicit charset: Charset = defaultCharset): ManagedResource[ZipInputStream]
- def zipOutputStream(implicit openOptions: OpenOptions = File.OpenOptions.default, charset: Charset = defaultCharset): ManagedResource[ZipOutputStream]
-
def
zipTo(destination: File, compressionLevel: Int = Deflater.DEFAULT_COMPRESSION)(implicit charset: Charset = defaultCharset): destination.type
Zips this file (or directory)
Zips this file (or directory)
- destination
The destination file; Creates this if it does not exists
- returns
The destination zip file