public class ManFileExt extends Object
Modifier and Type | Class and Description |
---|---|
static class |
ManFileExt.OnErrorAction
Enum that can be used to specify behaviour of the `copyRecursively()` function
in exceptional conditions.
|
Modifier and Type | Field and Description |
---|---|
static int |
DEFAULT_BUFFER_SIZE |
Constructor and Description |
---|
ManFileExt() |
Modifier and Type | Method and Description |
---|---|
static boolean |
copyRecursively(File thiz,
File target) |
static boolean |
copyRecursively(File thiz,
File target,
boolean overwrite,
BiFunction<File,IOException,ManFileExt.OnErrorAction> onError) |
static boolean |
copyRecursively(File thiz,
File target,
boolean overwrite,
BiFunction<File,IOException,ManFileExt.OnErrorAction> onError,
Predicate<File> filter)
Copies this file with all its children to the specified destination
target path. |
static boolean |
copyRecursively(File thiz,
File target,
Predicate<File> filter) |
static File |
copyTo(File thiz,
File target)
Same as
copyTo(File, File, false, #DEFAULT_BUFFER_SIZE) |
static File |
copyTo(File thiz,
File target,
boolean overwrite,
int bufferSize)
Copies this file to the given
target file. |
static File |
createTempDir()
Same as
createTempDir("tmp", null, null) |
static File |
createTempDir(String prefix,
String suffix,
File directory)
Creates an empty directory in the specified
directory , using the given prefix and suffix to generate its name. |
static boolean |
deleteRecursively(File thiz)
Delete this file with all its children.
|
static boolean |
endsWith(File thiz,
File other)
Determines whether this file path ends with the path of
other file. |
static boolean |
endsWith(File thiz,
String other)
Determines whether this file belongs to the same root as
other
and ends with all components of other in the same order. |
static String |
getExtension(File thiz)
Returns the extension of this file (not including the dot), or an empty string if it doesn't have one.
|
static boolean |
isRooted(File thiz)
Determines whether this file has a root or it represents a relative path.
|
static String |
nameWithoutExtension(File thiz)
Returns file's name without an extension.
|
static File |
normalize(File thiz)
Removes all .
|
static List<File> |
normalize(List<File> segments) |
static File |
relativeTo(File thiz,
File base)
Calculates the relative path for this file from
base file. |
static File |
relativeToOrNull(File thiz,
File base)
Calculates the relative path for this file from
base file. |
static File |
relativeToOrSelf(File thiz,
File base)
Calculates the relative path for this file from
base file. |
static File |
resolve(File thiz,
File relative)
Adds
relative file to this, considering this as a directory. |
static File |
resolve(File thiz,
String relative)
Adds
relative name to this, considering this as a directory. |
static File |
resolveSibling(File thiz,
File relative)
Adds
relative file to this parent directory. |
static File |
resolveSibling(File thiz,
String relative)
Adds
relative name to this parent directory. |
static String |
slashPath(File thiz)
Returns
path of this File using the invariant separator '/' to
separate the names in the name sequence. |
static boolean |
startsWith(File thiz,
File other)
Determines whether this file belongs to the same root as
other
and starts with all components of other in the same order. |
static boolean |
startsWith(File thiz,
String other)
Determines whether this file belongs to the same root as
other
and starts with all components of other in the same order. |
static FilePathComponents |
toComponents(File thiz)
Splits the file into path components (the names of containing directories and the name of the file
itself) and returns the resulting collection of components.
|
static String |
toRelativeString(File thiz,
File base)
Calculates the relative path for this file from
base file. |
static FileTreeWalk |
walk(File thiz,
FileTreeWalk.FileWalkDirection direction)
Gets an iterable for visiting this directory and all its content.
|
static FileTreeWalk |
walkBottomUp(File thiz)
Gets a sequence for visiting this directory and all its content in bottom-up order.
|
static FileTreeWalk |
walkTopDown(File thiz)
Gets a sequence for visiting this directory and all its content in top-down order.
|
public static final int DEFAULT_BUFFER_SIZE
public static File createTempDir(String prefix, String suffix, File directory) throws IOException
directory
, using the given prefix
and suffix
to generate its name.
If prefix
is not specified then some unspecified name will be used.
If suffix
is not specified then ".tmp" will be used.
If directory
is not specified then the default temporary-file directory will be used.
IOException
- in case of input/output error.IllegalArgumentException
- if is shorter than three symbols.public static File createTempDir() throws IOException
createTempDir("tmp", null, null)
IOException
createTempDir(String, String, File)
public static String getExtension(File thiz)
public static String slashPath(File thiz)
path
of this File using the invariant separator '/' to
separate the names in the name sequence.public static String nameWithoutExtension(File thiz)
public static String toRelativeString(File thiz, File base)
base
file.
Note that the base
file is treated as a directory.
If this file matches the base
file, then an empty string will be returned.base
to this.IllegalArgumentException
- if this and base paths have different roots.public static File relativeTo(File thiz, File base)
base
file.
Note that the base
file is treated as a directory.
If this file matches the base
file, then a File
with empty path will be returned.base
to this.IllegalArgumentException
- if this and base paths have different roots.public static File relativeToOrSelf(File thiz, File base)
base
file.
Note that the base
file is treated as a directory.
If this file matches the base
file, then a File
with empty path will be returned.base
to this, or this
if this and base paths have different roots.public static File relativeToOrNull(File thiz, File base)
base
file.
Note that the base
file is treated as a directory.
If this file matches the base
file, then a File
with empty path will be returned.base
to this, or null
if this and base paths have different roots.public static FilePathComponents toComponents(File thiz)
public static boolean isRooted(File thiz)
Returns true
when this file has non-empty root.
public static File copyTo(File thiz, File target, boolean overwrite, int bufferSize)
target
file.
If some directories on a way to the target
are missing, they will be created.
If the target
file already exists, this function will fail unless overwrite
argument is set to true
.
When overwrite
is true
and target
is a directory, it is replaced only if it is empty.
If this file is a directory, it is copied without its content, i.e. an empty target
directory is created.
If you want to copy directory including its contents, use copyRecursively
.
The operation doesn't preserve copied file attributes such as creation/modification date, permissions, etc.
overwrite
- true
if destination overwrite is allowed.bufferSize
- the buffer size to use when copying.target
file.NoSuchFileException
- if the source file doesn't exist.FileAlreadyExistsException
- if the destination file already exists and 'rewrite' argument is set to false
.IOException
- if any errors occur while copying.public static File copyTo(File thiz, File target)
copyTo(File, File, false, #DEFAULT_BUFFER_SIZE)
copyTo(File, File, boolean, int)
public static boolean copyRecursively(File thiz, File target, boolean overwrite, BiFunction<File,IOException,ManFileExt.OnErrorAction> onError, Predicate<File> filter)
target
path.
If some directories on the way to the destination are missing, they will be created.
If this file path points to a single file, it will be copied to a file with the path target
.
If this file path points to a directory, its children will be copied to a directory with the path target
.
If the target
already exists, it will be deleted before copying when the overwrite
parameter permits so.
The operation doesn't preserve copied file attributes such as creation/modification date, permissions, etc.
If any errors occur during the copying, further actions will depend on the result of the call to `onError(File, IOException)` function, that will be called with arguments, specifying the file that caused the error and the exception itself. By default this function rethrows exceptions.
Exceptions that can be passed to the onError
function:
- NoSuchFileException - if there was an attempt to copy a non-existent file - FileAlreadyExistsException - if there is a conflict - AccessDeniedException - if there was an attempt to open a directory that didn't succeed. - IOException - if some problems occur when copying.
Note that if this function fails, partial copying may have taken place.
false
if the copying was terminated, true
otherwise.public static boolean copyRecursively(File thiz, File target, Predicate<File> filter)
public static boolean copyRecursively(File thiz, File target, boolean overwrite, BiFunction<File,IOException,ManFileExt.OnErrorAction> onError)
public static FileTreeWalk walk(File thiz, FileTreeWalk.FileWalkDirection direction)
direction
- walk direction, top-down (by default) or bottom-up.public static FileTreeWalk walkTopDown(File thiz)
public static FileTreeWalk walkBottomUp(File thiz)
public static boolean deleteRecursively(File thiz)
true
if the file or directory is successfully deleted, false
otherwise.public static boolean startsWith(File thiz, File other)
other
and starts with all components of other
in the same order.
So if other
has N components, first N components of this
must be the same as in other
.true
if this path starts with other
path, false
otherwise.public static boolean startsWith(File thiz, String other)
other
and starts with all components of other
in the same order.
So if other
has N components, first N components of this
must be the same as in other
.true
if this path starts with other
path, false
otherwise.public static boolean endsWith(File thiz, File other)
other
file.
If other
is rooted path it must be equal to this.
If other
is relative path then last N components of this
must be the same as all components in other
,
where N is the number of components in other
.
true
if this path ends with other
path, false
otherwise.public static boolean endsWith(File thiz, String other)
other
and ends with all components of other
in the same order.
So if other
has N components, last N components of this
must be the same as in other
.
For relative other
, this
can belong to any root.true
if this path ends with other
path, false
otherwise.public static File normalize(File thiz)
public static File resolve(File thiz, File relative)
relative
file to this, considering this as a directory.
If relative
has a root, relative
is returned back.
For instance, `File("/foo/bar").resolve(File("gav"))` is `File("/foo/bar/gav")`.
This function is complementary with relativeTo
,
so `f.resolve(g.relativeTo(f)) == g` should be always true
except for different roots case.relative
paths, or just relative
if it's absolute.public static File resolve(File thiz, String relative)
relative
name to this, considering this as a directory.
If relative
has a root, relative
is returned back.
For instance, `File("/foo/bar").resolve("gav")` is `File("/foo/bar/gav")`.relative
paths, or just relative
if it's absolute.public static File resolveSibling(File thiz, File relative)
relative
file to this parent directory.
If relative
has a root or this has no parent directory, relative
is returned back.
For instance, `File("/foo/bar").resolveSibling(File("gav"))` is `File("/foo/gav")`.relative
paths, or just relative
if it's absolute or this has no parent.public static File resolveSibling(File thiz, String relative)
relative
name to this parent directory.
If relative
has a root or this has no parent directory, relative
is returned back.
For instance, `File("/foo/bar").resolveSibling("gav")` is `File("/foo/gav")`.relative
paths, or just relative
if it's absolute or this has no parent.Copyright © 2024. All rights reserved.