For large number of lines that may not fit in memory, use printLines
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")
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")
If file does not exist (or is a directory) no change is done and the current file is returned
checksum of this file (or directory) in hex format
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
this
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)
Check if this directory contains this file
Check if this directory contains this file
If strict is false, it would return true for self.contains(self)
true if this is a directory and it contains this file
destination
Copies this file into the given directory
Copies this file into the given directory
the File referencing the new file created under destination
Create this directory and all its parents Unlike the JDK, this by default sanely handles the JDK-8130464 bug If you want default Java behaviour, use File.LinkOptions.noFollow
Create this directory
Create this file
Create this file.
Create this file. If it exists, don't do anything
If you want this file to be created as a directory instead, set this to true (false by default)
If you also want all the parents to be created from root to this file (false by default)
Deletes this file or directory Unless otherwise specified, this does not follow symlinks i.e.
Deletes this file or directory Unless otherwise specified, this does not follow symlinks i.e. if this is a symlink, only the symlink itself is deleted and not the linked object
If this is set to true, any exception thrown is swallowed
whether the dot should be included in the extension or not
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
to lowercase the extension or not e.g. foo.HTML should have .html or .HTML
extension of this file if it is a regular file and has an extension, else None
extension (including the dot) of this file if it is a regular file and has an extension, else None
Util to glob from this file's path
Util to glob from this file's path
If true, we don't need to set path glob patterns e.g. instead of *
Util to match from this file's path using Regex
Util to match from this file's path using Regex
If true, we don't need to set path glob patterns e.g. instead of *
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
true if this file (or the file found by following symlink) is a directory
true if file is not present or empty directory or 0-bytes file
Check if a file is locked.
Check if a file is locked.
The random access mode.
The position at which the locked region is to start; must be non-negative.
The size of the locked region; must be non-negative, and the sum position + size must be non-negative.
true to request a shared lock, false to request an exclusive lock.
True if the file is locked, false otherwise.
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
isOwnerReadable
true if this file (or the file found by following symlink) is a regular file
true if this file is exactly same as that file For directories, it checks for equivalent directory structure
Almost same as isSameContentAs but uses faster md5 hashing to compare (and thus small chance of false positive) Also works for directories
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()
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
all lines in this file
List files recursively up to given depth using a custom file filter
destination
Moves this file into the given directory
Moves this file into the given directory
the File referencing the new file created under destination
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
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
For files with multiple extensions e.g. "bundle.tar.gz" nameWithoutExtension(includeAll = true) returns "bundle" nameWithoutExtension(includeAll = false) returns "bundle.tar"
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
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
parentOption
Some(parent) of this file or None if this is the root and thus has no parent
Deserialize a object using Java's default serialization from this file
file size (for directories, return size of the directory) in bytes
Streamed unzipping is slightly slower but supports larger files and more encodings
Streamed unzipping is slightly slower but supports larger files and more encodings
destination folder; Creates this if it does not exist
The destination where contents are unzipped
https://github.com/pathikrit/better-files/issues/152
Some(target) if this is a symbolic link (to target) else None
test if file has this permission
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 lifetime
This util auto-deletes the resource when done using the Dispose facility
Example usage: File.temporaryDirectory().foreach(tempDir => doSomething(tempDir)
Similar to the UNIX command touch - create this file if it does not exist and set its last modification time
unzip to a temporary zip file
unzip to a temporary zip file
the zip file
Unzips this zip file
Unzips this zip file
destination folder; Creates this if it does not exist
An optional param to reject or accept unzipping a file
The destination where contents are unzipped
Set a file attribute e.g.
Set a file attribute e.g. file("dos:system") = true
Some(true) if file is guaranteed to exist Some(false) if file is guaranteed to not exist None if the status is unknown e.g. if file is unreadable
https://stackoverflow.com/questions/30520179/why-does-file-exists-return-true-even-though-files-exists-in-the-nio-files
https://docs.oracle.com/javase/tutorial/essential/io/check.html
Walk the directory tree recursively upto maxDepth
Walk the directory tree recursively upto maxDepth
List of children in BFS maxDepth level deep (includes self since self is at depth = 0)
Write byte array to file.
Write byte array to file. For large contents consider using the writeBytes
this
Serialize a object using Java's serializer into this file, creating it and its parents if they do not exist
zip to a temp directory
zip to a temp directory
the target directory
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"))
this
Zips this file (or directory)
Zips this file (or directory)
The destination file; Creates this if it does not exists
The destination zip file
Scala wrapper around java.nio.files.Path