Class ManFileExt
- java.lang.Object
-
- manifold.io.extensions.java.io.File.ManFileExt
-
public class ManFileExt extends Object
A collection of useful extensions to java.io.File. Partially adapted from kotlin.io.files.Utils.kt
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classManFileExt.OnErrorActionEnum that can be used to specify behaviour of the `copyRecursively()` function in exceptional conditions.
-
Field Summary
Fields Modifier and Type Field Description static intDEFAULT_BUFFER_SIZE
-
Constructor Summary
Constructors Constructor Description ManFileExt()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static booleancopyRecursively(File thiz, File target)static booleancopyRecursively(File thiz, File target, boolean overwrite, BiFunction<File,IOException,ManFileExt.OnErrorAction> onError)static booleancopyRecursively(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 destinationtargetpath.static booleancopyRecursively(File thiz, File target, Predicate<File> filter)static FilecopyTo(File thiz, File target)Same ascopyTo(File, File, false, #DEFAULT_BUFFER_SIZE)static FilecopyTo(File thiz, File target, boolean overwrite, int bufferSize)Copies this file to the giventargetfile.static FilecreateTempDir()Same ascreateTempDir("tmp", null, null)static FilecreateTempDir(String prefix, String suffix, File directory)Creates an empty directory in the specifieddirectory, using the givenprefixandsuffixto generate its name.static booleandeleteRecursively(File thiz)Delete this file with all its children.static booleanendsWith(File thiz, File other)Determines whether this file path ends with the path ofotherfile.static booleanendsWith(File thiz, String other)Determines whether this file belongs to the same root asotherand ends with all components ofotherin the same order.static StringgetExtension(File thiz)Returns the extension of this file (not including the dot), or an empty string if it doesn't have one.static booleanisRooted(File thiz)Determines whether this file has a root or it represents a relative path.static StringnameWithoutExtension(File thiz)Returns file's name without an extension.static Filenormalize(File thiz)Removes all . and resolves all possible .. in this file name.static List<File>normalize(List<File> segments)static FilerelativeTo(File thiz, File base)Calculates the relative path for this file frombasefile.static FilerelativeToOrNull(File thiz, File base)Calculates the relative path for this file frombasefile.static FilerelativeToOrSelf(File thiz, File base)Calculates the relative path for this file frombasefile.static Fileresolve(File thiz, File relative)Addsrelativefile to this, considering this as a directory.static Fileresolve(File thiz, String relative)Addsrelativename to this, considering this as a directory.static FileresolveSibling(File thiz, File relative)Addsrelativefile to this parent directory.static FileresolveSibling(File thiz, String relative)Addsrelativename to this parent directory.static StringslashPath(File thiz)Returnspathof this File using the invariant separator '/' to separate the names in the name sequence.static booleanstartsWith(File thiz, File other)Determines whether this file belongs to the same root asotherand starts with all components ofotherin the same order.static booleanstartsWith(File thiz, String other)Determines whether this file belongs to the same root asotherand starts with all components ofotherin the same order.static FilePathComponentstoComponents(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 StringtoRelativeString(File thiz, File base)Calculates the relative path for this file frombasefile.static FileTreeWalkwalk(File thiz, FileTreeWalk.FileWalkDirection direction)Gets an iterable for visiting this directory and all its content.static FileTreeWalkwalkBottomUp(File thiz)Gets a sequence for visiting this directory and all its content in bottom-up order.static FileTreeWalkwalkTopDown(File thiz)Gets a sequence for visiting this directory and all its content in top-down order.
-
-
-
Field Detail
-
DEFAULT_BUFFER_SIZE
public static final int DEFAULT_BUFFER_SIZE
- See Also:
- Constant Field Values
-
-
Method Detail
-
createTempDir
public static File createTempDir(String prefix, String suffix, File directory) throws IOException
Creates an empty directory in the specifieddirectory, using the givenprefixandsuffixto generate its name.If
prefixis not specified then some unspecified name will be used. Ifsuffixis not specified then ".tmp" will be used. Ifdirectoryis not specified then the default temporary-file directory will be used.- Returns:
- a file object corresponding to a newly-created directory.
- Throws:
IOException- in case of input/output error.IllegalArgumentException- if is shorter than three symbols.
-
createTempDir
public static File createTempDir() throws IOException
Same ascreateTempDir("tmp", null, null)- Throws:
IOException- See Also:
createTempDir(String, String, File)
-
getExtension
public 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.
-
slashPath
public static String slashPath(File thiz)
Returnspathof this File using the invariant separator '/' to separate the names in the name sequence.
-
nameWithoutExtension
public static String nameWithoutExtension(File thiz)
Returns file's name without an extension.
-
toRelativeString
public static String toRelativeString(File thiz, File base)
Calculates the relative path for this file frombasefile. Note that thebasefile is treated as a directory. If this file matches thebasefile, then an empty string will be returned.- Returns:
- relative path from
baseto this. - Throws:
IllegalArgumentException- if this and base paths have different roots.
-
relativeTo
public static File relativeTo(File thiz, File base)
Calculates the relative path for this file frombasefile. Note that thebasefile is treated as a directory. If this file matches thebasefile, then aFilewith empty path will be returned.- Returns:
- File with relative path from
baseto this. - Throws:
IllegalArgumentException- if this and base paths have different roots.
-
relativeToOrSelf
public static File relativeToOrSelf(File thiz, File base)
Calculates the relative path for this file frombasefile. Note that thebasefile is treated as a directory. If this file matches thebasefile, then aFilewith empty path will be returned.- Returns:
- File with relative path from
baseto this, orthisif this and base paths have different roots.
-
relativeToOrNull
public static File relativeToOrNull(File thiz, File base)
Calculates the relative path for this file frombasefile. Note that thebasefile is treated as a directory. If this file matches thebasefile, then aFilewith empty path will be returned.- Returns:
- File with relative path from
baseto this, ornullif this and base paths have different roots.
-
toComponents
public 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.
-
isRooted
public static boolean isRooted(File thiz)
Determines whether this file has a root or it represents a relative path.Returns
truewhen this file has non-empty root.
-
copyTo
public static File copyTo(File thiz, File target, boolean overwrite, int bufferSize)
Copies this file to the giventargetfile.If some directories on a way to the
targetare missing, they will be created. If thetargetfile already exists, this function will fail unlessoverwriteargument is set totrue.When
overwriteistrueandtargetis 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
targetdirectory is created. If you want to copy directory including its contents, usecopyRecursively.The operation doesn't preserve copied file attributes such as creation/modification date, permissions, etc.
- Parameters:
overwrite-trueif destination overwrite is allowed.bufferSize- the buffer size to use when copying.- Returns:
- the
targetfile. - Throws:
NoSuchFileException- if the source file doesn't exist.FileAlreadyExistsException- if the destination file already exists and 'rewrite' argument is set tofalse.IOException- if any errors occur while copying.
-
copyTo
public static File copyTo(File thiz, File target)
Same ascopyTo(File, File, false, #DEFAULT_BUFFER_SIZE)- See Also:
copyTo(File, File, boolean, int)
-
copyRecursively
public 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 destinationtargetpath. 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 pathtarget.If the
targetalready exists, it will be deleted before copying when theoverwriteparameter 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
onErrorfunction:- 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.
- Returns:
falseif the copying was terminated,trueotherwise.
-
copyRecursively
public static boolean copyRecursively(File thiz, File target, Predicate<File> filter)
-
copyRecursively
public static boolean copyRecursively(File thiz, File target, boolean overwrite, BiFunction<File,IOException,ManFileExt.OnErrorAction> onError)
-
walk
public static FileTreeWalk walk(File thiz, FileTreeWalk.FileWalkDirection direction)
Gets an iterable for visiting this directory and all its content.- Parameters:
direction- walk direction, top-down (by default) or bottom-up.
-
walkTopDown
public static FileTreeWalk walkTopDown(File thiz)
Gets a sequence for visiting this directory and all its content in top-down order. Depth-first search is used and directories are visited before all their files.
-
walkBottomUp
public static FileTreeWalk walkBottomUp(File thiz)
Gets a sequence for visiting this directory and all its content in bottom-up order. Depth-first search is used and directories are visited after all their files.
-
deleteRecursively
public static boolean deleteRecursively(File thiz)
Delete this file with all its children. Note that if this operation fails then partial deletion may have taken place.- Returns:
trueif the file or directory is successfully deleted,falseotherwise.
-
startsWith
public static boolean startsWith(File thiz, File other)
Determines whether this file belongs to the same root asotherand starts with all components ofotherin the same order. So ifotherhas N components, first N components ofthismust be the same as inother.- Returns:
trueif this path starts withotherpath,falseotherwise.
-
startsWith
public static boolean startsWith(File thiz, String other)
Determines whether this file belongs to the same root asotherand starts with all components ofotherin the same order. So ifotherhas N components, first N components ofthismust be the same as inother.- Returns:
trueif this path starts withotherpath,falseotherwise.
-
endsWith
public static boolean endsWith(File thiz, File other)
Determines whether this file path ends with the path ofotherfile.If
otheris rooted path it must be equal to this. Ifotheris relative path then last N components ofthismust be the same as all components inother, where N is the number of components inother.- Returns:
trueif this path ends withotherpath,falseotherwise.
-
endsWith
public static boolean endsWith(File thiz, String other)
Determines whether this file belongs to the same root asotherand ends with all components ofotherin the same order. So ifotherhas N components, last N components ofthismust be the same as inother. For relativeother,thiscan belong to any root.- Returns:
trueif this path ends withotherpath,falseotherwise.
-
normalize
public static File normalize(File thiz)
Removes all . and resolves all possible .. in this file name. For instance, `File("/foo/./bar/gav/../baaz").normalize()` is `File("/foo/bar/baaz")`.- Returns:
- normalized pathname with . and possibly .. removed.
-
resolve
public static File resolve(File thiz, File relative)
Addsrelativefile to this, considering this as a directory. Ifrelativehas a root,relativeis returned back. For instance, `File("/foo/bar").resolve(File("gav"))` is `File("/foo/bar/gav")`. This function is complementary withrelativeTo, so `f.resolve(g.relativeTo(f)) == g` should be alwaystrueexcept for different roots case.- Returns:
- concatenated this and
relativepaths, or justrelativeif it's absolute.
-
resolve
public static File resolve(File thiz, String relative)
Addsrelativename to this, considering this as a directory. Ifrelativehas a root,relativeis returned back. For instance, `File("/foo/bar").resolve("gav")` is `File("/foo/bar/gav")`.- Returns:
- concatenated this and
relativepaths, or justrelativeif it's absolute.
-
resolveSibling
public static File resolveSibling(File thiz, File relative)
Addsrelativefile to this parent directory. Ifrelativehas a root or this has no parent directory,relativeis returned back. For instance, `File("/foo/bar").resolveSibling(File("gav"))` is `File("/foo/gav")`.- Returns:
- concatenated this.parent and
relativepaths, or justrelativeif it's absolute or this has no parent.
-
resolveSibling
public static File resolveSibling(File thiz, String relative)
Addsrelativename to this parent directory. Ifrelativehas a root or this has no parent directory,relativeis returned back. For instance, `File("/foo/bar").resolveSibling("gav")` is `File("/foo/gav")`.- Returns:
- concatenated this.parent and
relativepaths, or justrelativeif it's absolute or this has no parent.
-
-