public final class FileUtility extends Object
FileUtility
provides Various file related utility functionality.Modifier and Type | Method and Description |
---|---|
static URL |
createNestedJarFileUrl(URL aJarUrl,
File aToDir)
Copies a file residing in a nested JAR to the given destination folder.
|
static URL |
getNestedJarFileUrl(URL aJarUrl,
File aToDir)
Determines whether an according destination file already exists for the
file residing in a nested JAR.
|
static InputStream |
getResourceAsStream(Class<?> aClass,
String aPath)
Provides an
InputStream for a resource found at the given path
relative to the given class file (which might be inside a Java archive
such as a JAR file or a WAR file). |
static String |
toFileNameFromZip(String aZipFileName)
Truncates the ".zip" suffix from the filename and returns the result.
|
static InputStream |
toInputStream(File aFile)
Returns an
InputStream from the provided File . |
static String[] |
toJarHierarchy(URL aJarUrl)
Takes an URL pointing into a (nested) JAR resources and returns a list of
JAR archive names (including the ".jar" suffix) in the order of their
nesting, the first JAR archive being the outermost (parent) archive and
the last JAR archive being the innermost archive.
|
static URL |
toNestedJarFileUrl(URL aJarUrl,
File aToDir)
Convenience method testing whether the given JAR file resource already
exists in the expected folder layout .Returns its URL in case it already
exists else it is being created and then the URL is returned.
|
static OutputStream |
toOutputStream(File aFile)
Returns an
OutputStream to the provided File . |
static URL |
toParentJarUrl()
Determines the parent JAR file's URL for your running application (not
including the "!"
|
static String |
toTempFileName()
Generates a file name for a temporary file consisting if the current time
in milliseconds and a portion of random character to avoid name clashes:
"temp-012345678901234567890123456789-abcdefgh".
|
public static InputStream getResourceAsStream(Class<?> aClass, String aPath)
InputStream
for a resource found at the given path
relative to the given class file (which might be inside a Java archive
such as a JAR file or a WAR file).aClass
- The class relative to which to look for the resource.aPath
- The path which to use relative to the given class.InputStream
for the requested resource.public static InputStream toInputStream(File aFile) throws ZipException, IOException, FileNotFoundException
InputStream
from the provided File
. In case
the file points to a ZIP compressed file, then the uncompressed data is
provided by the InputStream
.aFile
- The File
for which to get the InputStream
.InputStream
, in case of a ZIP compressed File
,
an uncompressed InputStream
is returned.ZipException
- in case there were problems when accessing the ZIP
compressed File
.IOException
- in case there were problems working with the
File
.FileNotFoundException
- in case there was none such File
found.public static OutputStream toOutputStream(File aFile) throws ZipException, IOException, FileNotFoundException
OutputStream
to the provided File
. In case the
file points to a ZIP compressed file (it has the file suffix ".zip"),
then the data written to the OutputStream
is being ZIP
compressed.aFile
- The File
for which to get the OutputStream
.OutputStream
, in case of a ZIP compressed File
was specified (with suffix ".zip"), then a compressed
OutputStream
is returned.ZipException
- in case there were problems when accessing the ZIP
compressed File
.IOException
- in case there were problems working with the
File
.FileNotFoundException
- in case there was none such File
found.public static String toFileNameFromZip(String aZipFileName)
aZipFileName
- The file name of the ZIP file for which to get the
"inner" file name.public static String toTempFileName()
public static URL createNestedJarFileUrl(URL aJarUrl, File aToDir) throws IOException
aJarUrl
- The URL which points into a (nested) JAR's resource.aToDir
- The base directory into which the (nested) JAR's resource
will be extracted.IOException
- in case processing the extracted file caused
problems, e.g. the target folder is not a directory, it is not
writable, the JAR archive caused problems (currpted) and so on.public static URL getNestedJarFileUrl(URL aJarUrl, File aToDir) throws IOException
toJarHierarchy(URL)
is assumed within that base folder (as
created by the createNestedJarFileUrl(URL, File)
) for preventing
side effects with files of the same name residing in different nested JAR
archives.aJarUrl
- The URL which points into a (nested) JAR's resource.aToDir
- The base directory in which the (nested) JAR's resource is
being expected.IOException
- Thrown in case no details on the referenced entry can
be retrieved from the addressed JAR archive.public static URL toNestedJarFileUrl(URL aJarUrl, File aToDir) throws IOException
aJarUrl
- The URL which points into a (nested) JAR's resource.aToDir
- The base directory in which the (nested) JAR's resource is
being expected (created).IOException
- Thrown in case no details on the referenced entry can
be retrieved from the addressed JAR archive.getNestedJarFileUrl(URL, File)
,
createNestedJarFileUrl(URL, File)
public static URL toParentJarUrl()
public static String[] toJarHierarchy(URL aJarUrl)
aJarUrl
- The URL for which to get the JAR file hierarchy array.Copyright © 2015. All rights reserved.