java.lang.Object
org.refcodes.io.FileUtility
The
FileUtility
provides Various file related utility functionality.-
Method Summary
Modifier and TypeMethodDescriptionstatic URL
createNestedJarFileUrl
(URL aJarUrl, File aToDir) Copies a file residing in a nested JAR to the given destination folder.static File
createRandomTempFile
(long aBytes) Creates a temporaryFile
with random content as of theRandomTextMode.ASCII
of the given size.static File
createRandomTempFile
(long aBytes, boolean isDeleteOnExit) Creates a temporaryFile
with random content as of theRandomTextMode
of the given size.static File
createRandomTempFile
(long aBytes, org.refcodes.textual.RandomTextMode aRandomTextMode) Creates a temporaryFile
with random content as of theRandomTextMode
of the given size.static File
createRandomTempFile
(long aBytes, org.refcodes.textual.RandomTextMode aRandomTextMode, boolean isDeleteOnExit) Creates a temporaryFile
with random content as of theRandomTextMode
of the given size.static File
createRandomTempFile
(String aBaseFileName, long aBytes) Creates a temporaryFile
with random content as of theRandomTextMode.ASCII
of the given size.static File
createRandomTempFile
(String aBaseFileName, long aBytes, boolean isDeleteOnExit) Creates a temporaryFile
with random content as of theRandomTextMode
of the given size.static File
createRandomTempFile
(String aBaseFileName, long aBytes, org.refcodes.textual.RandomTextMode aRandomTextMode) Creates a temporaryFile
with random content as of theRandomTextMode
of the given size.static File
createRandomTempFile
(String aBaseFileName, long aBytes, org.refcodes.textual.RandomTextMode aRandomTextMode, boolean isDeleteOnExit) Creates a temporaryFile
with random content as of theRandomTextMode
of the given size.static File
Creates an empty temporaryFile
.static File
createTempFile
(boolean isDeleteOnExit) Creates an empty temporaryFile
.static File
createTempFile
(long aBytes, byte aValue) Creates a temporaryFile
filled with content of the specified value with the given size (number of bytes).static File
createTempFile
(long aBytes, byte aValue, boolean isDeleteOnExit) Creates a temporaryFile
filled with content of the specified value with the given size (number of bytes).static File
createTempFile
(String aBaseFileName) Creates an empty temporaryFile
.static File
createTempFile
(String aBaseFileName, boolean isDeleteOnExit) Creates an empty temporaryFile
.static File
createTempFile
(String aBaseFileName, long aBytes, byte aValue) Creates a temporaryFile
filled with content of the specified value with the given size (number of bytes).static File
createTempFile
(String aBaseFileName, long aBytes, byte aValue, boolean isDeleteOnExit) Creates a temporaryFile
filled with content of the specified value with the given size (number of bytes).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 anInputStream
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 boolean
Determines equality of the provided files by comparing their content (payload).static File
openTempFile
(String aBaseFileName) Opens a temporary (existing)File
which has been created viacreateTempFile(String)
,createTempFile(String, boolean)
,createTempFile(String, long, byte)
orcreateTempFile(String, long, byte, boolean)
(make sure to use the same base filename when opening as has been used when creating).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 URL
Determines the parent JAR file's URL for your running application (not including the "!"static String
Generates a base name for a temporary file (excluding a file suffix) consisting if the current time in milliseconds and a portion of random characters to avoid name clashes: "temp-012345678901234567890123456789-abcdefgh".static String
Generates a file name for a temporary file consisting if the current time in milliseconds and a portion of random characters to avoid name clashes: "temp-012345678901234567890123456789-abcdefgh.tmp".
-
Method Details
-
isEqual
Determines equality of the provided files by comparing their content (payload).- Parameters:
aFileA
- The oneFile
to be compared with the otherFile
.aFileB
- The otherFile
to be compared with the oneFile
.- Returns:
- True in case of equality, else false.
- Throws:
FileNotFoundException
- thrown in case aFile
was not found.IOException
- thrown in case there were I/O related problems.
-
createNestedJarFileUrl
Copies a file residing in a nested JAR to the given destination folder. The provided folder represents the base folder, actually an unambiguous folder layout is created within that base folder to prevent side effects with files of the same name residing in different nested JAR archives.- Parameters:
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.- Returns:
- The file URL (protocol "file:") for the extracted resource addressed in the (nested) JAR or null in case we do not have a JAR URL. In case we already have a file URL then the URL is returned untouched ignoring any passed destination folder (the to-dir argument).
- Throws:
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.- See Also:
-
- "https://docs.jboss.org/jbossas/javadoc/4.0.2/org/jboss/util/file/JarUtils.java.html"
-
createRandomTempFile
Creates a temporaryFile
with random content as of theRandomTextMode.ASCII
of the given size. The temporaryFile
is deleted upon exiting the JVM.- Parameters:
aBytes
- The size in bytes of theFile
to be created.- Returns:
- The temporary
File
of the given random content and size. - Throws:
IOException
- thrown in case there were I/O related problems creating the file.
-
createRandomTempFile
Creates a temporaryFile
with random content as of theRandomTextMode
of the given size.- Parameters:
aBytes
- The size in bytes of theFile
to be created.isDeleteOnExit
- Delete the file upon exit of the JVM.- Returns:
- The temporary
File
of the given random content and size. - Throws:
IOException
- thrown in case there were I/O related problems creating the file.
-
createRandomTempFile
public static File createRandomTempFile(long aBytes, org.refcodes.textual.RandomTextMode aRandomTextMode) throws IOException Creates a temporaryFile
with random content as of theRandomTextMode
of the given size. The temporaryFile
is deleted upon exiting the JVM.- Parameters:
aBytes
- The size in bytes of theFile
to be created.aRandomTextMode
- TheRandomTextMode
to use when creating theFile
.- Returns:
- The temporary
File
of the given random content and size. - Throws:
IOException
- thrown in case there were I/O related problems creating the file.
-
createRandomTempFile
public static File createRandomTempFile(long aBytes, org.refcodes.textual.RandomTextMode aRandomTextMode, boolean isDeleteOnExit) throws IOException Creates a temporaryFile
with random content as of theRandomTextMode
of the given size.- Parameters:
aBytes
- The size in bytes of theFile
to be created.aRandomTextMode
- TheRandomTextMode
to use when creating theFile
.isDeleteOnExit
- Delete the file upon exit of the JVM.- Returns:
- The temporary
File
of the given random content and size. - Throws:
IOException
- thrown in case there were I/O related problems creating the file.
-
createRandomTempFile
Creates a temporaryFile
with random content as of theRandomTextMode.ASCII
of the given size. The temporaryFile
is deleted upon exiting the JVM.- Parameters:
aBaseFileName
- The base file name to use.aBytes
- The size in bytes of theFile
to be created.- Returns:
- The temporary
File
of the given random content and size. - Throws:
IOException
- thrown in case there were I/O related problems creating the file.
-
createRandomTempFile
public static File createRandomTempFile(String aBaseFileName, long aBytes, boolean isDeleteOnExit) throws IOException Creates a temporaryFile
with random content as of theRandomTextMode
of the given size.- Parameters:
aBaseFileName
- The base file name to use.aBytes
- The size in bytes of theFile
to be created.isDeleteOnExit
- Delete the file upon exit of the JVM.- Returns:
- The temporary
File
of the given random content and size. - Throws:
IOException
- thrown in case there were I/O related problems creating the file.
-
createRandomTempFile
public static File createRandomTempFile(String aBaseFileName, long aBytes, org.refcodes.textual.RandomTextMode aRandomTextMode) throws IOException Creates a temporaryFile
with random content as of theRandomTextMode
of the given size. The temporaryFile
is deleted upon exiting the JVM.- Parameters:
aBaseFileName
- The base file name to use.aBytes
- The size in bytes of theFile
to be created.aRandomTextMode
- TheRandomTextMode
to use when creating theFile
.- Returns:
- The temporary
File
of the given random content and size. - Throws:
IOException
- thrown in case there were I/O related problems creating the file.
-
createRandomTempFile
public static File createRandomTempFile(String aBaseFileName, long aBytes, org.refcodes.textual.RandomTextMode aRandomTextMode, boolean isDeleteOnExit) throws IOException Creates a temporaryFile
with random content as of theRandomTextMode
of the given size.- Parameters:
aBaseFileName
- The base file name to use.aBytes
- The size in bytes of theFile
to be created.aRandomTextMode
- TheRandomTextMode
to use when creating theFile
.isDeleteOnExit
- Delete the file upon exit of the JVM.- Returns:
- The temporary
File
of the given random content and size. - Throws:
IOException
- thrown in case there were I/O related problems creating the file.
-
createTempFile
Creates a temporaryFile
filled with content of the specified value with the given size (number of bytes). The temporaryFile
is deleted upon exiting the JVM.- Parameters:
aBytes
- The size in bytes of theFile
to be created.aValue
- The value with which to fill the file.- Returns:
- The temporary
File
of the given content and size. - Throws:
IOException
- thrown in case there were I/O related problems creating the file.
-
createTempFile
public static File createTempFile(long aBytes, byte aValue, boolean isDeleteOnExit) throws IOException Creates a temporaryFile
filled with content of the specified value with the given size (number of bytes). T- Parameters:
aBytes
- The size in bytes of theFile
to be created.aValue
- The value with which to fill the file.isDeleteOnExit
- Delete the file upon exit of the JVM.- Returns:
- The temporary
File
of the given content and size. - Throws:
IOException
- thrown in case there were I/O related problems creating the file.
-
createTempFile
public static File createTempFile(String aBaseFileName, long aBytes, byte aValue) throws IOException Creates a temporaryFile
filled with content of the specified value with the given size (number of bytes). The temporaryFile
is deleted upon exiting the JVM.- Parameters:
aBaseFileName
- The base file name to use.aBytes
- The size in bytes of theFile
to be created.aValue
- The value with which to fill the file.- Returns:
- The temporary
File
of the given content and size. - Throws:
IOException
- thrown in case there were I/O related problems creating the file.
-
createTempFile
public static File createTempFile(String aBaseFileName, long aBytes, byte aValue, boolean isDeleteOnExit) throws IOException Creates a temporaryFile
filled with content of the specified value with the given size (number of bytes).- Parameters:
aBaseFileName
- The base file name to use.aBytes
- The size in bytes of theFile
to be created.aValue
- The value with which to fill the file.isDeleteOnExit
- Delete the file upon exit of the JVM.- Returns:
- The temporary
File
of the given content and size. - Throws:
IOException
- thrown in case there were I/O related problems creating the file.
-
createTempFile
- Returns:
- The temporary
File
. - Throws:
IOException
- thrown in case there were I/O related problems creating the file.
-
createTempFile
Creates an empty temporaryFile
.- Parameters:
isDeleteOnExit
- Delete the file upon exit of the JVM.- Returns:
- The temporary
File
. - Throws:
IOException
- thrown in case there were I/O related problems creating the file.
-
createTempFile
- Parameters:
aBaseFileName
- The base file name to use.- Returns:
- The temporary
File
. - Throws:
IOException
- thrown in case there were I/O related problems creating the file.
-
createTempFile
Creates an empty temporaryFile
.- Parameters:
aBaseFileName
- The base file name to use.isDeleteOnExit
- Delete the file upon exit of the JVM.- Returns:
- The temporary
File
. - Throws:
IOException
- thrown in case there were I/O related problems creating the file.
-
openTempFile
Opens a temporary (existing)File
which has been created viacreateTempFile(String)
,createTempFile(String, boolean)
,createTempFile(String, long, byte)
orcreateTempFile(String, long, byte, boolean)
(make sure to use the same base filename when opening as has been used when creating).- Parameters:
aBaseFileName
- The base file name of the file to use.- Returns:
- The according
File
. - Throws:
IOException
- thrown in case there were I/O related problems opening the file.
-
getNestedJarFileUrl
Determines whether an according destination file already exists for the file residing in a nested JAR. The provided folder represents the base folder; actually an unambiguous folder layout as oftoJarHierarchy(URL)
is assumed within that base folder (as created by thecreateNestedJarFileUrl(URL, File)
) for preventing side effects with files of the same name residing in different nested JAR archives.- Parameters:
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.- Returns:
- The file URL (protocol "file:") for the identified (existing) resource addressed in the (nested) JAR or null in case there is no such file in the expected folder layout. In case we already have a file URL then the URL is returned untouched ignoring any passed destination folder (the to-dir argument).
- Throws:
IOException
- Thrown in case no details on the referenced entry can be retrieved from the addressed JAR archive.- See Also:
-
- "https://docs.jboss.org/jbossas/javadoc/4.0.2/org/jboss/util/file/JarUtils.java.html"
-
getResourceAsStream
Provides anInputStream
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).- Parameters:
aClass
- The class relative to which to look for the resource.aPath
- The path which to use relative to the given class.- Returns:
- The
InputStream
for the requested resource.
-
toJarHierarchy
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. A JAR "path" of an URL might look as follows: "jar:file:/home/steiner/Workspaces/com.fightclub/fightclub-app/target/fightclub-app-0.0.1-SNAPSHOT.jar!/webapp/home.xhtml" "jar:file:/home/steiner/Workspaces/com.fightclub/fightclub-app/target/fightclub-app-0.0.1-SNAPSHOT.jar!/lib/fightclub-adapter-web-0.0.1-SNAPSHOT.jar!/webapp/home.xhtml"- Parameters:
aJarUrl
- The URL for which to get the JAR file hierarchy array.- Returns:
- The array with the JAR archive hierarchy or null if not being a JAR URL.
-
toNestedJarFileUrl
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.- Parameters:
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).- Returns:
- The parrent's JAR file URL or null if the application does not seem to reside in a JAR. The file URL (protocol "file:") for the existing (created) resource addressed in the (nested) JAR.
- Throws:
IOException
- Thrown in case no details on the referenced entry can be retrieved from the addressed JAR archive.- See Also:
-
toParentJarUrl
Determines the parent JAR file's URL for your running application (not including the "!" which is required to address a file within that JAR).- Returns:
- The parrent's JAR file URL or null if the application does not seem to reside in a JAR.
-
toTempBaseFileName
Generates a base name for a temporary file (excluding a file suffix) consisting if the current time in milliseconds and a portion of random characters to avoid name clashes: "temp-012345678901234567890123456789-abcdefgh".- Returns:
- A temp file name.
-
toTempFileName
Generates a file name for a temporary file consisting if the current time in milliseconds and a portion of random characters to avoid name clashes: "temp-012345678901234567890123456789-abcdefgh.tmp".- Returns:
- A temp file name.
-