object util
Useful file-related utility functions.
- Alphabetic
- By Inheritance
- util
- 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
-
final
def
==(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
final
def
asInstanceOf[T0]: T0
- Definition Classes
- Any
-
def
basename(path: String, fileSep: String = fileSeparator): String
Get the basename (file name only) part of a path.
Get the basename (file name only) part of a path.
- path
the path (absolute or relative)
- fileSep
the file separator to use. Defaults to the value of the "file.separator" property.
- returns
the file name portion
-
def
clone(): AnyRef
- Attributes
- protected[java.lang]
- Definition Classes
- AnyRef
- Annotations
- @native() @throws( ... )
-
def
copy(file: String, targetDir: String): Try[Boolean]
Copy a file to a directory.
Copy a file to a directory. If the target directory does not exist, it is created. The JDK's java.nio.file.Files.copy() function provides a similar capability.
- file
Path name of the file to copy
- targetDir
Path name to target directory
- returns
Success(true)
if the copy worked.Failure(exception)
on error.
-
def
copy(file: String, targetDir: String, createTarget: Boolean): Try[Boolean]
Copy a file to a directory.
Copy a file to a directory. The JDK's java.nio.file.Files.copy() function provides a similar capability.
- file
Path name of the file to copy
- targetDir
Path name to target directory
- createTarget
true
to create the target directory,false
to throw an exception if the directory doesn't already exist.- returns
Success(true)
if the copy worked.Failure(exception)
on error.
-
def
copy(files: Iterable[String], targetDir: String, createTarget: Boolean = true): Try[Boolean]
Copy multiple files to a target directory.
Copy multiple files to a target directory. Also see the version of this method that takes only one file.
- files
An
Iterable
of file names to be copied- targetDir
Path name to target directory
- createTarget
true
to create the target directory,false
to throw an exception if the directory doesn't already exist.- returns
Success(true)
if the copy worked.Failure(exception)
on error.
- Annotations
- @SuppressWarnings()
-
def
copyFile(source: File, target: File): Try[File]
Copy a source file to a target file, using binary copying.
Copy a source file to a target file, using binary copying. The source file must be a file. The target path can be a file or a directory; if it is a directory, the target file will have the same base name as as the source file.
The JDK's java.nio.file.Files.copy() function provides a similar capability.
- source
path to the source file
- target
path to the target file or directory
- returns
A
Success
containing the full path of the target file, orFailure(exception)
-
def
copyFile(sourcePath: String, targetPath: String): Try[String]
Copy a source file to a target file, using binary copying.
Copy a source file to a target file, using binary copying. The source file must be a file. The target path can be a file or a directory; if it is a directory, the target file will have the same base name as as the source file.
The JDK's java.nio.file.Files.copy() function provides a similar capability.
- sourcePath
path to the source file
- targetPath
path to the target file or directory
- returns
A
Success
with the full path of the target file, orFailure(exception)
-
def
copyTree(sourceDir: File, targetDir: File): Try[Boolean]
Recursively copy a source directory and its contents to a target directory.
Recursively copy a source directory and its contents to a target directory. Creates the target directory if it does not exist.
- sourceDir
the source directory
- targetDir
the target directory
- returns
Success(true)
if the copy worked.Failure(exception)
on error.
- Annotations
- @SuppressWarnings()
-
def
copyTree(sourceDir: String, targetDir: String): Try[Boolean]
Recursively copy a source directory and its contents to a target directory.
Recursively copy a source directory and its contents to a target directory. Creates the target directory if it does not exist.
- sourceDir
the source directory
- targetDir
the target directory
- returns
Success(true)
if the copy worked.Failure(exception)
on error.
-
def
deleteTree(dir: File): Try[Int]
Recursively remove a directory tree.
Recursively remove a directory tree. This function is conceptually equivalent to
rm -r
on a Unix system.- dir
The directory
- returns
Failure(exception)
on error,Success(total)
on success.total
is the total number of deleted files.
-
def
deleteTree(dir: String): Unit
Recursively remove a directory tree.
Recursively remove a directory tree. This function is conceptually equivalent to
rm -r
on a Unix system.- dir
The directory
-
def
dirname(path: String, fileSep: String = fileSeparator): String
Get the directory name of a pathname.
Get the directory name of a pathname.
- path
path (absolute or relative)
- fileSep
the file separator to use. Defaults to the value of the "file.separator" property.
- returns
the directory portion
-
def
dirnameBasename(path: String, fileSep: String = fileSeparator): (String, String)
Split a path into directory (dirname) and file (basename) components.
Split a path into directory (dirname) and file (basename) components. Analogous to Python's
os.path.pathsplit()
function.- path
the path to split
- fileSep
the file separator to use. Defaults to the value of the "file.separator" property.
- returns
a (dirname, basename) tuple of strings
-
def
dirnameBasenameExtension(pathname: String, fileSep: String = fileSeparator): (String, String, String)
Split a pathname into the directory name, basename, and extension pieces.
Split a pathname into the directory name, basename, and extension pieces.
- pathname
the pathname
- fileSep
the file separator to use. Defaults to the value of the "file.separator" property.
- returns
a 3-tuple of (dirname, basename, extension)
-
def
eglob(pattern: String): List[String]
An extended glob function that supports all the wildcards of the
glob()
function, in addition to:An extended glob function that supports all the wildcards of the
glob()
function, in addition to:- a leading
~
, signifying the user's home directory - a special
**
wildcard that recursively matches any directory. (Think "ant".)
~user is not supported, however.
NOTE: Any non-Windows operating system is treated as Posix.
- pattern
the wildcard pattern
- returns
list of matches, or an empty list for none
- a leading
-
final
def
eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
-
def
equals(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- val fileSeparator: String
- val fileSeparatorChar: Char
-
def
finalize(): Unit
- Attributes
- protected[java.lang]
- Definition Classes
- AnyRef
- Annotations
- @throws( classOf[java.lang.Throwable] )
-
def
fnmatch(name: String, pattern: String): Boolean
Similar to Python's
fnmatch()
function, this function determines whether a string matches a wildcard pattern.Similar to Python's
fnmatch()
function, this function determines whether a string matches a wildcard pattern. Patterns are Unix-style shell-style wildcards:*
matches everything?
matches any single character[set]
matches any character in set[!set]
matches any character not in set
An initial period in
filename
is not special. Matches are case-sensitive on Posix operating systems, case-insensitive elsewhere.- name
the name to match
- pattern
the wildcard pattern
-
final
def
getClass(): Class[_]
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
-
def
glob(path: String): List[String]
Return a list of paths matching a pathname pattern.
Return a list of paths matching a pathname pattern. The pattern may contain simple shell-style wildcards. See
fnmatch()
. This function is essentially a direct port of the Pythonglob.glob()
function.Restrictions:
- There's currently no way to escape a wildcard character. That is, if you need to match a '*' character or a '?' character exactly, you can't do that with this library. (You can't do it with the
glob
library in Python 2 or Python 3, either.)- path
The path to expand.
- returns
a list of possibly expanded file names
-
def
hashCode(): Int
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
-
final
def
isInstanceOf[T0]: Boolean
- Definition Classes
- Any
-
def
joinAndNormalizePath(pieces: File*): File
Join components of a path together, using the current file separator; then, normalize the result.
Join components of a path together, using the current file separator; then, normalize the result.
- pieces
path pieces
- returns
a composite, normalized path
-
def
joinAndNormalizePath(pieces: String*): String
Join components of a path together, using the current file separator; then, normalize the result.
Join components of a path together, using the current file separator; then, normalize the result.
- pieces
path pieces
- returns
a composite, normalized path
-
def
joinPath(pieces: File*): File
Join components of a path together, using the current file separator.
Join components of a path together, using the current file separator.
- pieces
path pieces
- returns
a composite path
-
def
joinPath(pieces: String*): String
Join components of a path together, using the current file separator.
Join components of a path together, using the current file separator.
- pieces
path pieces
- returns
a composite path
-
def
joinPath(fileSep: String, pieces: Seq[String]): String
Join components of a path together, using the specified file separator.
Join components of a path together, using the specified file separator.
- fileSep
the file separator to use
- pieces
path pieces
- returns
a composite path
-
def
listRecursively(file: File, topdown: Boolean = true): LazyList[File]
List a directory recursively, returning
File
objects for each file (and subdirectory) found.List a directory recursively, returning
File
objects for each file (and subdirectory) found. This method does lazy evaluation, instead of calculating everything up-front, aswalk()
does.The JDK's java.nio.file.Files.walk() function provides a similar capability in JDK 8. Prior to JDK 8, you can also use java.nio.file.Files.walkFileTree()
- file
The
File
object, presumed to represent a directory.- topdown
If
true
(the default), the stream will be generated top down. Iffalse
, it'll be generated bottom-up.- returns
a stream of
File
objects.
-
def
longestCommonPathPrefix(paths: List[String]): String
Find the longest common path prefix from a list of paths.
Find the longest common path prefix from a list of paths. Based on https://rosettacode.org/wiki/Find_common_directory_path#Advanced
- paths
the paths
- returns
the longest common path, which might be the empty string
- Annotations
- @SuppressWarnings()
-
def
makeTemporaryDirectory(prefix: String, maxTries: Int = 3): Try[File]
Create a temporary directory.
Create a temporary directory.
- prefix
Prefix for directory name
- maxTries
Maximum number of times to try creating the directory before giving up.
- returns
A
Success
of the directory, or aFailure
with any error that occurs.
-
def
nativePath(path: String): String
Converts a path name from universal path notation to the operating system-specific format.
Converts a path name from universal path notation to the operating system-specific format. Universal path notation always uses a Unix-style "/" to separate path elements. A native path can be converted to a universal path via the
universal_path()
function. Note that on POSIX-compliant systems, this function simply returns thepath
parameter unmodified.- path
the path to convert from universtal to native path notation
- returns
the native path
-
final
def
ne(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
-
def
normalizePath(path: String): String
Normalize a path, eliminating double slashes, resolving embedded ".." strings (e.g., "/foo/../bar" becomes "/bar"), etc.
Normalize a path, eliminating double slashes, resolving embedded ".." strings (e.g., "/foo/../bar" becomes "/bar"), etc. Works for Windows and Posix operating systems.
- path
the path
- returns
the normalized path
-
def
normalizePosixPath(path: String): String
Adapted from the Python version of normpath() in Python's
os.posixpath
module.Adapted from the Python version of normpath() in Python's
os.posixpath
module.- path
the path
- returns
the normalized path
-
def
normalizeWindowsPath(path: String): String
Normalize a Windows path name.
Normalize a Windows path name. Handles UNC paths. Adapted from the Python version of normpath() in Python's
os.ntpath
module.- path
the path
- returns
the normalized path
-
final
def
notify(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
-
final
def
notifyAll(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
-
def
pwd: String
Return the current working directory, as an absolute path.
Return the current working directory, as an absolute path.
- returns
the current working directory
-
def
relativePath(from: File, to: File): String
Calculate the relative path between two files.
Calculate the relative path between two files.
- from
the starting file
- to
the file to be converted to a relative path
- returns
the (String) relative path
-
def
splitDrivePath(path: String): (String, String)
Split a Windows-style path into drive name and path portions.
Split a Windows-style path into drive name and path portions.
- path
the path
- returns
a (drive, path) tuple, either component of which can be * an empty string
-
def
splitPath(path: String, fileSep: String = fileSeparator): List[String]
Split a path into its constituent components.
Split a path into its constituent components. If the path is absolute, the first piece will have a file separator in the beginning. Examples:
Input Output "" List("") "/" List("/") "foo" List("foo") "foo/bar" List("foo", "bar") "." List(".") "../foo" List("..", "foo") "./foo" List(".", "foo") "/foo/bar/baz" List("/foo", "bar", "baz") "foo/bar/baz" List("foo", "bar", "baz") "/foo" List("/foo") - path
the path
- fileSep
the file separator to use. Defaults to the value of the "file.separator" property.
- returns
the component pieces.
-
final
def
synchronized[T0](arg0: ⇒ T0): T0
- Definition Classes
- AnyRef
-
def
temporaryDirectory: File
Determine the temporary directory to use.
Determine the temporary directory to use.
- returns
the temporary directory
-
def
toString(): String
- Definition Classes
- AnyRef → Any
-
def
touch(path: String, time: Long = -1): Try[Boolean]
Similar to the Unix
touch
command, this function:Similar to the Unix
touch
command, this function:- updates the access and modification times for a file
- creates the file if it does not exist
If the file is a directory, this method will return an error.
- path
The file to touch
- time
Set the last-modified time to this time, or to the current time if this parameter is negative.
- returns
Failure(exception)
on error,Success(true)
on success
-
def
touchMany(files: Iterable[String], time: Long = -1): Try[Int]
Similar to the Unix touch command, this function:
Similar to the Unix touch command, this function:
- updates the access and modification times for any existing files in a list of files
- creates an nonexistent files in the list
If any file in the list is a directory, this method will return an error.
- files
Iterable of files to touch
- time
Set the last-modified time to this time, or to the current time if this parameter is negative.
- returns
Failure(exception)
on error.Success(total)
on success.
-
def
universalPath(path: String): String
Converts a path name from its operating system-specific format to a universal path notation.
Converts a path name from its operating system-specific format to a universal path notation. Universal path notation always uses a Unix-style "/" to separate path elements. A universal path can be converted to a native (operating system-specific) path via the
native_path()
function. Note that on POSIX-compliant systems, this function simply returns thepath
parameter unmodified.- path
the path to convert to universal path notation
- returns
the universal path
-
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
- @native() @throws( ... )
-
def
walk(top: String, topdown: Boolean = true): List[(String, List[String], List[String])]
Directory tree generator, adapted from Python's
os.walk()
function.Directory tree generator, adapted from Python's
os.walk()
function.For each directory in the directory tree rooted at top (including top itself, but excluding '.' and '..'), yields a 3-tuple
(dirpath, dirnames, filenames)
dirpath is a string, the path to the directory. dirnames is a list of the names of the subdirectories in dirpath (excluding '.' and '..'). filenames is a list of the names of the non-directory files in dirpath. Note that the names in the lists are just names, with no path components. To get a full path (which begins with top) to a file or directory in dirpath, use
dirpath + java.io.fileSeparator + name
, or usejoinPath()
.If topdown is
true
, the triple for a directory is generated before the triples for any of its subdirectories (directories are generated top down). Iftopdown
isfalse
, the triple for a directory is generated after the triples for all of its subdirectories (directories are generated bottom up).WARNING! This method does not grok symbolic links!
The JDK's java.nio.file.Files.walk() function provides a similar capability in JDK 8. Prior to JDK 8, you can also use java.nio.file.Files.walkFileTree()
- top
name of starting directory
- topdown
true
to do a top-down traversal,false
otherwise.- returns
List of triplets, as described above.
-
def
withTemporaryDirectory[T](prefix: String)(action: (File) ⇒ T): T
Allow execution of a block of code within the context of a temporary directory.
Allow execution of a block of code within the context of a temporary directory. The temporary directory is cleaned up after the operation completes.
- prefix
file name prefix to use
- action
action to perform
- returns
whatever the action returns. Errors are thrown as
IOException
.
- Annotations
- @SuppressWarnings()
Deprecated Value Members
-
def
createTemporaryDirectory(prefix: String, maxTries: Int = 3): File
Create a temporary directory.
Create a temporary directory. Note: This function is deprecated, in favor of makeTemporaryDirectory, which does not throw exceptions.
- prefix
Prefix for directory name
- maxTries
Maximum number of times to try creating the directory before giving up.
- returns
the directory. Throws an IOException if it can't create the directory.
- Annotations
- @deprecated @SuppressWarnings()
- Deprecated
(Since version 4.5.0) Use makeTemporaryDirectory