public final class FileTools
extends java.lang.Object
| Modifier and Type | Field and Description |
|---|---|
static java.io.FileFilter |
CLASS_PATH_ENTRY_FILTER
Deprecated.
No longer of use when walking file trees via visitor.
|
static java.lang.String |
ILLEGAL_CHARS
A string containing characters illegal for file names.
|
| Modifier and Type | Method and Description |
|---|---|
static java.util.List<java.lang.String> |
addAllEntriesForClassPath(java.io.File file,
java.util.List<java.lang.String> classPath,
java.util.logging.Logger logger)
Deprecated.
Use
addAllEntriesForClassPath(Path, List, Logger) instead. |
static java.util.List<java.lang.String> |
addAllEntriesForClassPath(java.nio.file.Path path,
java.util.List<java.lang.String> classPath,
java.util.logging.Logger logger)
Adds all entries making up a classpath (JAR-files and directories) below the designated tree.
|
static void |
cleanDirectory(java.io.File directory)
Deprecated.
Use
deleteTree(Path, boolean) instead. |
static <C extends java.io.Closeable> |
close(C closeable)
Closes the given
Closeable and returns null. |
static void |
closeQuietly(java.io.Closeable... closeables)
Closes the given
Closeables, swallowing a potential
IOException. |
static void |
closeQuietly(java.io.Closeable closeable)
Closes the given
Closeable, swallowing a potential
IOException. |
static void |
copy(java.io.File src,
java.io.File dest)
Deprecated.
Use
copy(Path, FilenameFilter, Path, CopyOption...) instead. |
static void |
copy(java.io.File src,
java.io.FilenameFilter filter,
java.io.File dest)
Deprecated.
Use
copy(Path, FilenameFilter, Path, CopyOption...) instead. |
static void |
copy(java.io.File src,
java.io.FilenameFilter filter,
java.io.File dest,
boolean keepExecutable)
Deprecated.
Use
copy(Path, FilenameFilter, Path, CopyOption...) instead. |
static void |
copy(java.nio.file.Path src,
java.io.FilenameFilter filter,
java.nio.file.Path dest,
java.nio.file.CopyOption... options)
Copies the designated source path to the designated destination path.
|
static void |
createFileWithParents(java.nio.file.Path path)
Creates the designated file including all parent directories.
|
static java.io.File |
createTempFile(java.io.File parent,
java.lang.String child,
boolean directory)
Creates a new temporary file or directory as child of the designated
directory.
|
static void |
delete(java.io.File file)
Deprecated.
Use
deleteTree(Path, boolean) instead. |
static boolean |
deleteRecursive(java.io.File path)
Deprecated.
Use
deleteTree(Path, boolean) instead. |
static boolean |
deleteTree(java.nio.file.Path tree,
boolean includeRoot)
Deletes the designated file path recursively, that is, if it is a directory, its complete
contents will be deleted.
|
static void |
fileAccessible(java.io.File file,
java.lang.String usedFor)
Checks whether the designated file can be accessed (written to).
|
static java.lang.String |
findFileAtDefaultLocation(java.lang.String[] relativePaths,
java.lang.String[] absolutePaths)
Tries to find one of the given relative or absolute paths and returns the
first match.
|
static java.net.URL[] |
getAllEntriesForClassPath(java.io.File file,
java.util.logging.Logger logger)
Deprecated.
Use
getAllEntriesForClassPath(Path, Logger) instead. |
static java.net.URL[] |
getAllEntriesForClassPath(java.nio.file.Path path,
java.util.logging.Logger logger)
Gets all entries making up a classpath (JAR-files and directories) below the designated tree.
|
static java.io.File |
getDir(java.lang.String path,
boolean create)
Checks if the designated path directory is a valid directory, it exists or
can be created and returns it.
|
static java.lang.String |
getExtension(java.io.File file)
Returns the filename's extension, i.e. the characters after the last dot.
|
static java.lang.String |
getExtension(java.lang.String filename)
Returns the file's extension, i.e. the characters after the last dot.
|
static java.io.File |
getWithoutExtension(java.io.File file)
Returns the given file without the filename extension.
|
static java.lang.String |
getWithoutExtension(java.lang.String path)
Returns the given file path without the filename extension.
|
static boolean |
hasExtension(java.io.File file,
java.lang.String extension)
Tests whether the given file has the given extension.
|
static java.net.URL |
pathPropertyToURL(java.lang.String classPath,
java.util.logging.Logger logger)
Converts the designated classpath into an URL.
|
static java.net.URL[] |
pathPropertyToURLs(java.util.List<java.lang.String> classPaths,
java.util.logging.Logger logger)
Converts the designated classpaths into an array of URLs by using
pathPropertyToURL(String, Logger). |
static java.nio.file.Path |
relativise(java.nio.file.Path base,
java.nio.file.Path target)
Gets the path of
target relative to the designated base. |
static java.nio.file.Path |
relativise(java.lang.String base,
java.lang.String target)
Gets the path of
target relative to the designated base. |
static java.lang.String |
relativize(java.io.File base,
java.io.File file)
Deprecated.
Use
relativise(Path, Path) instead. |
static java.lang.String |
relativize(java.lang.String base,
java.lang.String file)
Deprecated.
Use
relativise(String, String) instead. |
static java.lang.String |
sanitise(java.lang.String filename,
java.lang.String replacement)
Sanitises the name of a file (i.e. without any path) by replacing every
illegal character with the given string.
|
static byte[] |
toByteArray(java.io.File file)
Reads a file into a new byte array.
|
static void |
unzip(java.io.File zipFile,
java.io.File targetDir)
Unzips the designated zip file to the designated target folder.
|
static boolean |
urlAccessible(java.net.URL url,
java.util.logging.Logger logger,
boolean write)
Checks (more or less reliable) whether the designated URL (directory)
exists and can be accessed (for reading and/or writing).
|
public static final java.lang.String ILLEGAL_CHARS
@Deprecated public static final java.io.FileFilter CLASS_PATH_ENTRY_FILTER
public static java.lang.String getExtension(java.io.File file)
file - the file for which to get the extensionpublic static java.lang.String getExtension(java.lang.String filename)
filename - the filename for which to get the extensionpublic static boolean hasExtension(java.io.File file,
java.lang.String extension)
file - the file whose extension should be testedextension - the extension to test forpublic static java.io.File getWithoutExtension(java.io.File file)
file - the file from which to remove the extensionpublic static java.lang.String getWithoutExtension(java.lang.String path)
path - the file path from which to remove the extension@Deprecated
public static java.lang.String relativize(java.lang.String base,
java.lang.String file)
relativise(String, String) instead.file relative to the given base
directory.base - the base for the relative pathfile - the file or directory for which to determine a relative pathbase + File.separator +
relative path denotes file; or null if
there is no relative path possible (e.g. on Windows when base and
file don't share the same drive letter)@Deprecated
public static java.lang.String relativize(java.io.File base,
java.io.File file)
relativise(Path, Path) instead.file relative to the given base
directory.base - the base for the relative pathfile - the file or directory for which to determine a relative pathbase + File.separator +
relative path denotes file; or null if
there is no relative path possible (e.g. on Windows when base and
file don't share the same drive letter)public static java.nio.file.Path relativise(java.lang.String base,
java.lang.String target)
target relative to the designated base.
base and target must either both be absolute or both be relative.base - The path as base for the relative path.target - The file or directory for which to determine a relative path relative to
base.base to target. This
will be null if no relative path is possible.public static java.nio.file.Path relativise(java.nio.file.Path base,
java.nio.file.Path target)
target relative to the designated base.
base and target must either both be absolute or both be relative.base - The path as base for the relative path.target - The file or directory for which to determine a relative path relative to
base.base to target. This
will be null if no relative path is possible.public static java.lang.String sanitise(java.lang.String filename,
java.lang.String replacement)
filename - the filename to sanitisereplacement - the replacement for illegal characterspublic static java.io.File createTempFile(java.io.File parent,
java.lang.String child,
boolean directory)
throws java.io.IOException
IOException will be thrown.parent - The parent for the directory or file to be created. If this
is null, the system temp dir will be used as parent.child - The name of the temporary directory or file to be created.
This must either be one directory or one file. Do not use several
directories or a file and one or more directories. If you need
this, call this method for each directory and the file.directory - Whether the temporary file system object is a directory
(or a file).java.io.IOException - If the temporary
IOException will be thrown.public static <C extends java.io.Closeable> C close(C closeable)
throws java.io.IOException
Closeable and returns null.C - closeable - the Closeable to be closednulljava.io.IOException - if Closeable.close() throws an exceptionpublic static void closeQuietly(java.io.Closeable closeable)
Closeable, swallowing a potential
IOException.closeable - the Closeable to be closed; may be nullpublic static void closeQuietly(java.io.Closeable... closeables)
Closeables, swallowing a potential
IOException.closeables - the Closeables to be closed; may be nullpublic static byte[] toByteArray(java.io.File file)
throws java.io.IOException
file - the file whose content should be loaded into a byte arrayjava.io.IOException - if an I/O error occurs or the file was not foundpublic static java.net.URL[] pathPropertyToURLs(java.util.List<java.lang.String> classPaths,
java.util.logging.Logger logger)
pathPropertyToURL(String, Logger).classPaths - The classpaths to add as URLs or relative files.logger - The logger for log information created by this method.URI.isAbsolute()public static java.net.URL pathPropertyToURL(java.lang.String classPath,
java.util.logging.Logger logger)
For instance, [/C:/Program%20Files/Java/jre1.5.0_07/lib/, /var/lib/security/local_policy.jar, http://aristaflow.de/adept2.jar] is converted to
classPath - The classpath to as URL or relative files.logger - The logger for log information created by this method.URI.isAbsolute()public static java.io.File getDir(java.lang.String path,
boolean create)
path - The directory to check.create - Whether to create the directory if it does not exist.File-instance representing the designated
configuration directory or null in case the directory
does not exist, cannot be accessed or cannot be created if
appropriate.public static void fileAccessible(java.io.File file,
java.lang.String usedFor)
throws java.io.IOException
file - The file to check for whether it can be accessed (written to).usedFor - A string providing information for what the file is used
for.java.io.IOException - If the designated file cannot be accessed (written to)
or cannot be created, an IOException will be thrown.public static void createFileWithParents(java.nio.file.Path path)
throws java.io.IOException
path - The file which to create.java.io.IOException - If there are problems creating the file or its parent directories, an
IOException will be thrown.@Deprecated
public static boolean deleteRecursive(java.io.File path)
throws java.io.FileNotFoundException
deleteTree(Path, boolean) instead.path - The path which to delete recursively.java.io.FileNotFoundException - If the designated path (or one of its
children, for instance when deleting concurrently) is not found,
a FileNotFoundException will be thrown.public static boolean deleteTree(java.nio.file.Path tree,
boolean includeRoot)
throws java.io.IOException
Files.exists(Path, java.nio.file.LinkOption...) prior to this method is recommended.tree - The tree which to delete recursively.includeRoot - Whether to also delete the designated tree root.java.io.IOException - If a problems occurs when deleting the designated tree, an
IOException will be thrown.public static boolean urlAccessible(java.net.URL url,
java.util.logging.Logger logger,
boolean write)
url - The URL to check.logger - The logger for logging messages related to the accessibility of the designated
URL. If this is null, no messages will be logged.write - Whether the designated URL should also be accessible for
writing.@Deprecated
public static java.net.URL[] getAllEntriesForClassPath(java.io.File file,
java.util.logging.Logger logger)
getAllEntriesForClassPath(Path, Logger) instead.CLASS_PATH_ENTRY_FILTER
and directories) below the designated file recursively. This is done in a
breadth-first order, that is, all subdirectories are added before their
content is added.file - The file of which to determine all entries making up a
classpath.logger - The logger where problems with accessing a directory will be
logged.public static java.net.URL[] getAllEntriesForClassPath(java.nio.file.Path path,
java.util.logging.Logger logger)
path - The tree of which to determine all entries making up a classpath.logger - The logger where problems with iterating the tree be logged.@Deprecated
public static java.util.List<java.lang.String> addAllEntriesForClassPath(java.io.File file,
java.util.List<java.lang.String> classPath,
java.util.logging.Logger logger)
addAllEntriesForClassPath(Path, List, Logger) instead.CLASS_PATH_ENTRY_FILTER
and directories) below the designated file recursively. This is done in a
breadth-first order, that is, all subdirectories are added before their
content is added.file - The file of which to determine all entries making up a
classpath.classPath - The classpath determined so far and where to add new
classpath entries.logger - The logger where problems with accessing a directory will be
logged.classPath).public static java.util.List<java.lang.String> addAllEntriesForClassPath(java.nio.file.Path path,
java.util.List<java.lang.String> classPath,
java.util.logging.Logger logger)
path - The tree of which to determine all entries making up a classpath. This path will
not be part of the classpath.classPath - The classpath where to add new classpath entries.logger - The logger where problems with iterating the tree be logged.classPath).public static java.lang.String findFileAtDefaultLocation(java.lang.String[] relativePaths,
java.lang.String[] absolutePaths)
relativePaths - Paths to search for, relative to the parent directory
of the current working directory.absolutePaths - Absolute paths to search for.public static void copy(java.nio.file.Path src,
java.io.FilenameFilter filter,
java.nio.file.Path dest,
java.nio.file.CopyOption... options)
throws java.io.IOException
StandardCopyOptions for replacing files in the destination and/or
copying file attributes (e. g. timestamps).src - The source file or directory which to copy, must not be null.filter - The filter a file from the source directory needs to match to be copied to the
target directory.dest - The destination file or directory to which to copy, must not be null.options - The copy options like replacing files in the destination or copying file
attributes.java.io.IOException - If there are problems transferring a file, an IOException will
be thrown.@Deprecated
public static void copy(java.io.File src,
java.io.File dest)
throws java.io.IOException
copy(Path, FilenameFilter, Path, CopyOption...) instead.src - The source file or directory which to copy, must not be
null.dest - The destination file or directory to which to copy, must not be
null.java.io.IOException - If there are problems transferring a file, an
IOException will be thrown.copy(File, FilenameFilter, File, boolean)@Deprecated
public static void copy(java.io.File src,
java.io.FilenameFilter filter,
java.io.File dest)
throws java.io.IOException
copy(Path, FilenameFilter, Path, CopyOption...) instead.null all
directories and files will be copied. The target may only be a file in case
the source is a file too. If source is a directory it will be copied
recursively to the target directory. src - The source file or directory which to copy, must not be
null.filter - The filter a file from the source directory needs to match to
be copied to the target directory.dest - The destination file or directory to which to copy, must not be
null.java.io.IOException - If there are problems transferring a file, an
IOException will be thrown.@Deprecated
public static void copy(java.io.File src,
java.io.FilenameFilter filter,
java.io.File dest,
boolean keepExecutable)
throws java.io.IOException
copy(Path, FilenameFilter, Path, CopyOption...) instead.null all
directories and files will be copied. The target may only be a file in case
the source is a file too. If source is a directory it will be copied
recursively to the target directory. If executable is to be kept and a source file is executable, the corresponding target file will become executable after copying.
src - The source file or directory which to copy, must not be
null.filter - The filter a file from the source directory needs to match to
be copied to the target directory.dest - The destination file or directory to which to copy, must not be
null.keepExecutable - Whether to take over File.canExecute() from
the source to the target.java.io.IOException - If there are problems transferring a file, an
IOException will be thrown.@Deprecated
public static void delete(java.io.File file)
throws java.io.IOException
deleteTree(Path, boolean) instead.file - The file or directory which to delete.java.io.IOException - If the designated file or directory cannot be deleted,
an IOException will be thrown.@Deprecated
public static void cleanDirectory(java.io.File directory)
throws java.io.IOException
deleteTree(Path, boolean) instead.directory - directory to cleanjava.io.IOException - in case cleaning is unsuccessful
Inspired by org.apache.commons.io.FileUtils.cleanDirectory(File)public static void unzip(java.io.File zipFile,
java.io.File targetDir)
throws java.io.IOException
zipFile - The zip file which to unzip.targetDir - The target folder to which to extract the content of the
designated zip file.java.io.IOException - If there are problems reading from the zip file or
writing to the target directory or the designated file is a
directory or the target directory is a file, an
IOException will be thrown.