public final class FileUtils extends Object
Modifier and Type | Field and Description |
---|---|
static int |
DEFAULT_SHORTENER_THRESHOLD |
static String |
SHORTENER_BACKSLASH |
static String |
SHORTENER_BACKSLASH_REGEX |
static String |
SHORTENER_ELLIPSE |
static String |
SHORTENER_SLASH |
static String |
SHORTENER_SLASH_REGEX |
Modifier and Type | Method and Description |
---|---|
static Collection<URL> |
appendContents(Collection<URL> result,
URL directory)
Recursively adds contents from the referenced directory to an existing
collection.
|
static Collection<URL> |
appendContents(Collection<URL> result,
URL directory,
boolean recurse,
boolean filesOnly)
Add contents from the referenced directory to an existing collection.
|
static File |
createTemporaryDirectory(String prefix)
Creates a temporary directory.
|
static File |
createTemporaryDirectory(String prefix,
String suffix)
Creates a temporary directory.
|
static File |
createTemporaryDirectory(String prefix,
String suffix,
File directory)
Creates a temporary directory.
|
static boolean |
deleteRecursively(File directory)
Deletes a directory recursively.
|
static Map<String,URL> |
findResources(String regex,
Iterable<URL> urls)
Finds
URL s of resources known to the system. |
static Map<String,URL> |
findResources(String regex,
String pathPrefix,
File baseDirectory)
Finds
URL s of available resources. |
static File[] |
getAllVersions(File directory,
String filename)
Lists all versions of a given (possibly versioned) file name.
|
static String |
getExtension(File file)
Extracts the file extension from a file.
|
static String |
getExtension(String path)
Extracts the file extension from a file path.
|
static Date |
getModifiedTime(File file)
Gets the
Date of the file's last modification. |
static String |
getPath(File file)
Gets the absolute path to the given file, with the directory separator
standardized to forward slash, like most platforms use.
|
static String |
getPath(String path,
String separator)
Gets a standardized path based on the given one, with the directory
separator standardized from the specific separator to forward slash, like
most platforms use.
|
static String |
limitPath(String path,
int limit)
Compacts a path into a given number of characters.
|
static Collection<URL> |
listContents(URL directory)
Recursively lists the contents of the referenced directory.
|
static Collection<URL> |
listContents(URL directory,
boolean recurse,
boolean filesOnly)
Lists all contents of the referenced directory.
|
static Matcher |
matchVersionedFilename(String filename)
Deprecated.
|
static byte[] |
readFile(File file)
Reads the contents of the given file into a new byte array.
|
static String |
shortenPath(String path)
Shortens the path to a maximum of 4 path elements.
|
static String |
shortenPath(String path,
int threshold)
Shortens the path based on the given maximum number of path elements.
|
static String |
stripFilenameVersion(String filename) |
static File |
urlToFile(String url)
Converts the given URL string to its corresponding
File . |
static File |
urlToFile(URL url)
|
static void |
writeFile(File file,
byte[] bytes)
Writes the given byte array to the specified file.
|
public static final int DEFAULT_SHORTENER_THRESHOLD
public static final String SHORTENER_BACKSLASH_REGEX
public static final String SHORTENER_SLASH_REGEX
public static final String SHORTENER_BACKSLASH
public static final String SHORTENER_SLASH
public static final String SHORTENER_ELLIPSE
public static String getPath(File file)
file
- The file whose path will be obtained and standardized.public static String getPath(String path, String separator)
path
- The path to standardize.separator
- The directory separator to be standardized.public static String getExtension(File file)
file
- the file objectpublic static String getExtension(String path)
path
- the path to the file (relative or absolute)public static Date getModifiedTime(File file)
Date
of the file's last modification.public static byte[] readFile(File file) throws IOException
IOException
- If the file cannot be read.To convert a byte array to a string.
public static void writeFile(File file, byte[] bytes) throws IOException
IOException
- If the file cannot be written.To convert a string to a byte array.
public static File[] getAllVersions(File directory, String filename)
directory
- the directory to scanfilename
- the file name to usepublic static File urlToFile(URL url)
URL
to its corresponding File
.
This method is similar to calling new File(url.toURI())
except that
it also handles "jar:file:" URLs, returning the path to the JAR file.
url
- The URL to convert.FileInputStream
IllegalArgumentException
- if the URL does not correspond to a file.public static File urlToFile(String url)
File
.url
- The URL to convert.FileInputStream
IllegalArgumentException
- if the URL does not correspond to a file.public static String shortenPath(String path)
path
- the path to the file (relative or absolute)public static String shortenPath(String path, int threshold)
path
- the path to the file (relative or absolute)threshold
- the number of directories to keep unshortenedpublic static String limitPath(String path, int limit)
path
- the path to the file (relative or absolute)limit
- the number of characters to which the path should be limitedpublic static File createTemporaryDirectory(String prefix) throws IOException
Since there is no atomic operation to do that, we create a temporary file, delete it and create a directory in its place. To avoid race conditions, we use the optimistic approach: if the directory cannot be created, we try to obtain a new temporary file rather than erroring out.
It is the caller's responsibility to make sure that the directory is
deleted; see deleteRecursively(File)
.
prefix
- The prefix string to be used in generating the file's name;
see File.createTempFile(String, String, File)
IOException
public static File createTemporaryDirectory(String prefix, String suffix) throws IOException
Since there is no atomic operation to do that, we create a temporary file, delete it and create a directory in its place. To avoid race conditions, we use the optimistic approach: if the directory cannot be created, we try to obtain a new temporary file rather than erroring out.
It is the caller's responsibility to make sure that the directory is
deleted; see deleteRecursively(File)
.
prefix
- The prefix string to be used in generating the file's name;
see File.createTempFile(String, String, File)
suffix
- The suffix string to be used in generating the file's name;
see File.createTempFile(String, String, File)
IOException
public static File createTemporaryDirectory(String prefix, String suffix, File directory) throws IOException
Since there is no atomic operation to do that, we create a temporary file, delete it and create a directory in its place. To avoid race conditions, we use the optimistic approach: if the directory cannot be created, we try to obtain a new temporary file rather than erroring out.
It is the caller's responsibility to make sure that the directory is
deleted; see deleteRecursively(File)
.
prefix
- The prefix string to be used in generating the file's name;
see File.createTempFile(String, String, File)
suffix
- The suffix string to be used in generating the file's name;
see File.createTempFile(String, String, File)
directory
- The directory in which the file is to be created, or null
if the default temporary-file directory is to be usedIOException
public static boolean deleteRecursively(File directory)
directory
- The directory to delete.File.delete()
)public static Collection<URL> listContents(URL directory)
file
and
jar
.directory
- The directory whose contents should be listed.URL
s representing the directory's contents.listContents(URL, boolean, boolean)
public static Collection<URL> listContents(URL directory, boolean recurse, boolean filesOnly)
file
and jar
.directory
- The directory whose contents should be listed.recurse
- Whether to list contents recursively, as opposed to only the
directory's direct contents.filesOnly
- Whether to exclude directories in the resulting collection
of contents.URL
s representing the directory's contents.public static Collection<URL> appendContents(Collection<URL> result, URL directory)
file
and jar
.result
- The collection to which contents should be added.directory
- The directory whose contents should be listed.URL
s representing the directory's contents.appendContents(Collection, URL, boolean, boolean)
public static Collection<URL> appendContents(Collection<URL> result, URL directory, boolean recurse, boolean filesOnly)
file
and jar
.result
- The collection to which contents should be added.directory
- The directory whose contents should be listed.recurse
- Whether to append contents recursively, as opposed to only
the directory's direct contents.filesOnly
- Whether to exclude directories in the resulting collection
of contents.URL
s representing the directory's contents.public static Map<String,URL> findResources(String regex, String pathPrefix, File baseDirectory)
URL
s of available resources. Both JAR files and files on disk
are searched, according to the following mechanism:
pathPrefix
are discovered using
ClassLoader.getResources(String)
with the current thread's context
class loader. In particular, this invocation discovers resources in JAR
files beneath the given pathPrefix
.pathPrefix
beneath the given
baseDirectory
is scanned last, so that users can more easily
override resources provided inside JAR files by placing a resource of the
same name within that directory.
In both cases, resources are then recursively scanned using
listContents(URL)
, and anything matching the given regex
pattern is added to the output map.
regex
- The regex to use when matching resources, or null to match
everything.pathPrefix
- The path to search for resources.baseDirectory
- The baseDirectory/pathPrefix
directory to scan
after the URL resources.AppUtils.getBaseDirectory(java.lang.String, java.lang.Class<?>, java.lang.String)
public static Map<String,URL> findResources(String regex, Iterable<URL> urls)
URL
s of resources known to the system.
Each of the given URL
s is recursively scanned using
listContents(URL)
, and anything matching the given regex
pattern is added to the output map.
regex
- The regex to use when matching resources, or null to match
everything.urls
- Paths to search for resources.@Deprecated public static Matcher matchVersionedFilename(String filename)
stripFilenameVersion(String)
Matcher
object dissecting a versioned file name.filename
- the file nameMatcher
objectCopyright © 2015–2022 SciJava. All rights reserved.