public final class FileTool
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
static java.util.Comparator<java.io.File> |
BY_NAME_CASE_INSENSITIVE
Sort by name (case insensitive).
|
static java.util.Comparator<java.io.File> |
BY_NAME_CASE_SENSITIVE
Sort by name (case sensitive).
|
static java.util.Comparator<java.io.File> |
DIRECTORIES_FIRST
Sort directories first.
|
static long |
NO_DIFFERENCE
Return value of
firstDiff(File, File) when both contents are equal. |
Modifier and Type | Method and Description |
---|---|
static void |
append(java.io.File file,
byte[] data)
Append the given bytes to a file.
|
static void |
append(java.io.File file,
byte[] data,
int offset,
int length)
Append the given bytes to a file.
|
static void |
append(java.lang.String file,
byte[] data)
Append the given bytes to a file.
|
static void |
append(java.lang.String file,
byte[] data,
int offset,
int length)
Append the given bytes to a file.
|
static boolean |
cleanup(java.io.File dir)
Cleanup a directory by removing its content.
|
static boolean |
contentEquals(java.io.File file1,
java.io.File file2)
Is the content of two files equal?
|
static boolean |
contentEquals(java.lang.String filePath1,
java.lang.String filePath2)
Is the content of two files equal?
|
static boolean |
deleteTree(java.io.File file)
Delete a file or file tree.
|
static java.io.File |
expand(java.io.File dir,
java.lang.String... subPath)
Append multiple path items to a file.
|
static long |
firstDiff(java.io.File file1,
java.io.File file2)
Get the position of the first difference of the content of two files.
|
static long |
firstDiff(java.lang.String filePath1,
java.lang.String filePath2)
Get the position of the first difference of the content of two files.
|
static java.lang.String |
getExtension(java.io.File file)
Get the extension of the file name.
|
static byte[] |
load(java.io.File file)
Load a file into memory.
|
static byte[] |
load(java.lang.String file)
Load a file into memory.
|
static java.io.File |
multiExpand(java.io.File dir,
char fileSeparator,
java.lang.String subPath)
Append a complete path to a file.
|
static java.io.File |
multiExpand(java.io.File dir,
java.lang.String subPath)
Append a complete path to a file.
|
static void |
store(java.io.File file,
byte[] data)
Store the given bytes to a file.
|
static void |
store(java.io.File file,
byte[] data,
int offset,
int length)
Store the given bytes to a file.
|
static void |
store(java.lang.String file,
byte[] data)
Store the given bytes to a file.
|
static void |
store(java.lang.String file,
byte[] data,
int offset,
int length)
Store the given bytes to a file.
|
static void |
walkTree(java.io.File file,
java.util.function.Consumer<? super java.io.File> fileHandler)
Walk a file tree.
|
static void |
walkTree(java.io.File file,
java.util.function.Predicate<? super java.io.File> stepIntoDir,
java.util.function.Consumer<? super java.io.File> fileHandler)
Walk a file tree.
|
static <E extends java.lang.Exception> |
walkTreeFragile(java.io.File file,
FragileProcedure1<E,? super java.io.File> fileHandler)
Walk a file tree and apply a consumer which might throw an exception.
|
static <E extends java.lang.Exception> |
walkTreeFragile(java.io.File file,
java.util.function.Predicate<java.io.File> stepIntoDir,
FragileProcedure1<E,? super java.io.File> fileHandler)
Walk a file tree and apply a consumer which might throw an exception.
|
static void |
walkTreeOrdered(java.io.File file,
java.util.function.Consumer<? super java.io.File> fileHandler,
java.util.Comparator<? super java.io.File> order)
Walk a file tree in a defined order.
|
static void |
walkTreeOrdered(java.io.File file,
java.util.function.Predicate<? super java.io.File> stepIntoDir,
java.util.function.Consumer<? super java.io.File> fileHandler,
java.util.Comparator<? super java.io.File> order)
Walk a file tree in a defined order.
|
public static final java.util.Comparator<java.io.File> DIRECTORIES_FIRST
Ignores names.
Use Comparator.thenComparing(Comparator)
with
BY_NAME_CASE_INSENSITIVE
or
BY_NAME_CASE_SENSITIVE
to get directories
first, while names are ordered inside each group.
Use Comparator.reversed()
for the reverse of this.
public static final java.util.Comparator<java.io.File> BY_NAME_CASE_INSENSITIVE
Directories and files are mixed.
Use Comparator.reversed()
to reverse this order.
DIRECTORIES_FIRST
public static final java.util.Comparator<java.io.File> BY_NAME_CASE_SENSITIVE
Directories and files are mixed.
Use Comparator.reversed()
to reverse this order.
DIRECTORIES_FIRST
public static final long NO_DIFFERENCE
firstDiff(File, File)
when both contents are equal.public static void walkTree(@NotNull java.io.File file, @NotNull java.util.function.Predicate<? super java.io.File> stepIntoDir, @NotNull java.util.function.Consumer<? super java.io.File> fileHandler)
file
- file to start with, usually a directorystepIntoDir
- Predicate allowing to avoid directories.
Always called with a directory, i.e.
file.isDirectory()
is always true
.fileHandler
- Handler called for each file.
File is never a directory, i.e.
file.isDirectory()
is always false
.public static void walkTree(@NotNull java.io.File file, @NotNull java.util.function.Consumer<? super java.io.File> fileHandler)
file
- file to start with, usually a directoryfileHandler
- Handler called for each file.
File is never a directory, i.e.
file.isDirectory()
is always false
.public static void walkTreeOrdered(@NotNull java.io.File file, @NotNull java.util.function.Predicate<? super java.io.File> stepIntoDir, @NotNull java.util.function.Consumer<? super java.io.File> fileHandler, @NotNull java.util.Comparator<? super java.io.File> order)
file
- file to start with, usually a directorystepIntoDir
- Predicate allowing to avoid directories.
Always called with a directory, i.e.
file.isDirectory()
is always true
.fileHandler
- Handler called for each file.
File is never a directory, i.e.
file.isDirectory()
is always false
.order
- comparator defining the order in which files inside a
directory are handled. Common orders can be defined
by combining the comparators DIRECTORIES_FIRST
,
BY_NAME_CASE_INSENSITIVE
, and
BY_NAME_CASE_SENSITIVE
.public static void walkTreeOrdered(@NotNull java.io.File file, @NotNull java.util.function.Consumer<? super java.io.File> fileHandler, @NotNull java.util.Comparator<? super java.io.File> order)
file
- file to start with, usually a directoryfileHandler
- Handler called for each file.
File is never a directory, i.e.
file.isDirectory()
is always false
.order
- comparator defining the order in which files inside a
directory are handled. Common orders can be defined
by combining the comparators DIRECTORIES_FIRST
,
BY_NAME_CASE_INSENSITIVE
, and
BY_NAME_CASE_SENSITIVE
.public static <E extends java.lang.Exception> void walkTreeFragile(@NotNull java.io.File file, @NotNull java.util.function.Predicate<java.io.File> stepIntoDir, @NotNull FragileProcedure1<E,? super java.io.File> fileHandler) throws E extends java.lang.Exception
E
- exception thrown by fragile procedure, forwarded by this methodfile
- file to start with, usually a directorystepIntoDir
- Predicate allowing to avoid directories.
Always called with a directory, i.e.
file.isDirectory()
is always true
,
before stepping into that directory.fileHandler
- Handler called for each file.
File is never a directory, i.e.
file.isDirectory()
is always false
.E
- when fileHandler
throws itE extends java.lang.Exception
public static <E extends java.lang.Exception> void walkTreeFragile(@NotNull java.io.File file, @NotNull FragileProcedure1<E,? super java.io.File> fileHandler) throws E extends java.lang.Exception
E
- exception thrown by fragile procedure, forwarded by this methodfile
- file to start with, usually a directoryfileHandler
- Handler called for each file.
File is never a directory, i.e.
file.isDirectory()
is always false
.E
- when fileHandler
throws itE extends java.lang.Exception
@Nullable public static java.lang.String getExtension(@NotNull java.io.File file)
file
- file with possible extensionnull
if the name does contain
any dotpublic static boolean contentEquals(@NotNull java.io.File file1, @NotNull java.io.File file2) throws java.io.IOException
file1
- first filefile2
- second filetrue
if both files have the same contentfalse
if both files have different contentjava.io.IOException
- on file open or read errorspublic static boolean contentEquals(@NotNull java.lang.String filePath1, @NotNull java.lang.String filePath2) throws java.io.IOException
filePath1
- path of first filefilePath2
- path of second filetrue
if both files have the same contentfalse
if both files have different contentjava.io.IOException
- on file open or read errorspublic static long firstDiff(@NotNull java.io.File file1, @NotNull java.io.File file2) throws java.io.IOException
file1
- first filefile2
- second fileNO_DIFFERENCE
if both files have the same contentjava.io.IOException
- on file open or read errorspublic static long firstDiff(@NotNull java.lang.String filePath1, @NotNull java.lang.String filePath2) throws java.io.IOException
filePath1
- path of first filefilePath2
- path of second fileNO_DIFFERENCE
if both files have the same contentjava.io.IOException
- on file open or read errors@NotNull public static byte[] load(@NotNull java.io.File file) throws java.io.IOException
file
- file to loadjava.io.IOException
- on read errors or if the file is too large to fit into a byte arrayload(String)
@NotNull public static byte[] load(@NotNull java.lang.String file) throws java.io.IOException
file
- file to loadjava.io.IOException
- on read errors or if the file is too large to fit into a byte arrayload(File)
public static void store(@NotNull java.io.File file, @NotNull byte[] data) throws java.io.IOException
file
- file to write todata
- data to writejava.io.IOException
- on write errorspublic static void store(@NotNull java.io.File file, @NotNull byte[] data, int offset, int length) throws java.io.IOException
file
- file to write todata
- data to writeoffset
- start offset into the byte arraylength
- number of bytes to be writtenjava.io.IOException
- on write errorspublic static void store(@NotNull java.lang.String file, @NotNull byte[] data) throws java.io.IOException
file
- file to write todata
- data to writejava.io.IOException
- on write errorspublic static void store(@NotNull java.lang.String file, @NotNull byte[] data, int offset, int length) throws java.io.IOException
file
- file to write todata
- data to writeoffset
- start offset into the byte arraylength
- number of bytes to be writtenjava.io.IOException
- on write errorspublic static void append(@NotNull java.io.File file, @NotNull byte[] data) throws java.io.IOException
file
- file to append todata
- data to appendjava.io.IOException
- on write errorspublic static void append(@NotNull java.io.File file, @NotNull byte[] data, int offset, int length) throws java.io.IOException
file
- file to append todata
- data to appendoffset
- start offset into the byte arraylength
- number of bytes to be writtenjava.io.IOException
- on write errorspublic static void append(@NotNull java.lang.String file, @NotNull byte[] data) throws java.io.IOException
file
- file to append todata
- data to appendjava.io.IOException
- on write errorspublic static void append(@NotNull java.lang.String file, @NotNull byte[] data, int offset, int length) throws java.io.IOException
file
- file to append todata
- data to appendoffset
- the start offset into the byte arraylength
- number of bytes to be writtenjava.io.IOException
- on write errorspublic static boolean deleteTree(@NotNull java.io.File file)
If removing fails for whatever reason, further processing is stop
and false
returned.
file
- file or directorytrue
if deletion is completefalse
if deletion of the file or the tree (partially) failedpublic static boolean cleanup(@NotNull java.io.File dir)
dir
is not a directory, nothing will be deleted.
If removing fails for whatever reason, further processing is stop
and false
returned.dir
- directorytrue
if cleanup is completefalse
if cleanup of the directory (partially) failed@NotNull public static java.io.File multiExpand(@NotNull java.io.File dir, @NotNull java.lang.String subPath)
dir
- directory where paths aresubPath
- sub path, has to be relativepublic static java.io.File multiExpand(@NotNull java.io.File dir, char fileSeparator, @NotNull java.lang.String subPath)
dir
- directory where paths arefileSeparator
- file separator char, usually either '/'
or `\\`
subPath
- sub path, has to be relativepublic static java.io.File expand(@NotNull java.io.File dir, @NotNull java.lang.String... subPath)
dir
- directory where paths aresubPath
- The sub path. User has to take care that the sub paths are valid
file names and don't contain separator characters.