Class FileUtility
java.lang.Object
org.refcodes.io.FileUtility
The
FileUtility provides Various file related utility functionality.-
Method Summary
Modifier and TypeMethodDescriptionstatic URLcreateNestedJarFileUrl(URL aJarUrl, File aToDir) Copies a file residing in a nested JAR to the given destination folder.static FilecreateRandomTempFile(long aBytes) Creates a temporaryFilewith random content as of theRandomTextMode.ASCIIof the given size.static FilecreateRandomTempFile(long aBytes, boolean isDeleteOnExit) Creates a temporaryFilewith random content as of theRandomTextModeof the given size.static FilecreateRandomTempFile(long aBytes, org.refcodes.textual.RandomTextMode aRandomTextMode) Creates a temporaryFilewith random content as of theRandomTextModeof the given size.static FilecreateRandomTempFile(long aBytes, org.refcodes.textual.RandomTextMode aRandomTextMode, boolean isDeleteOnExit) Creates a temporaryFilewith random content as of theRandomTextModeof the given size.static FilecreateRandomTempFile(String aBaseFileName, long aBytes) Creates a temporaryFilewith random content as of theRandomTextMode.ASCIIof the given size.static FilecreateRandomTempFile(String aBaseFileName, long aBytes, boolean isDeleteOnExit) Creates a temporaryFilewith random content as of theRandomTextModeof the given size.static FilecreateRandomTempFile(String aBaseFileName, long aBytes, org.refcodes.textual.RandomTextMode aRandomTextMode) Creates a temporaryFilewith random content as of theRandomTextModeof the given size.static FilecreateRandomTempFile(String aBaseFileName, long aBytes, org.refcodes.textual.RandomTextMode aRandomTextMode, boolean isDeleteOnExit) Creates a temporaryFilewith random content as of theRandomTextModeof the given size.static FileCreates an empty temporaryFile.static FilecreateTempFile(boolean isDeleteOnExit) Creates an empty temporaryFile.static FilecreateTempFile(long aBytes, byte aValue) Creates a temporaryFilefilled with content of the specified value with the given size (number of bytes).static FilecreateTempFile(long aBytes, byte aValue, boolean isDeleteOnExit) Creates a temporaryFilefilled with content of the specified value with the given size (number of bytes).static FilecreateTempFile(String aBaseFileName) Creates an empty temporaryFile.static FilecreateTempFile(String aBaseFileName, boolean isDeleteOnExit) Creates an empty temporaryFile.static FilecreateTempFile(String aBaseFileName, long aBytes, byte aValue) Creates a temporaryFilefilled with content of the specified value with the given size (number of bytes).static FilecreateTempFile(String aBaseFileName, long aBytes, byte aValue, boolean isDeleteOnExit) Creates a temporaryFilefilled with content of the specified value with the given size (number of bytes).static URLgetNestedJarFileUrl(URL aJarUrl, File aToDir) Determines whether an according destination file already exists for the file residing in a nested JAR.static InputStreamgetResourceAsStream(Class<?> aClass, String aPath) Provides anInputStreamfor 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 booleanDetermines equality of the provided files by comparing their content (payload).static FileopenTempFile(String aBaseFileName) Opens a temporary (existing)Filewhich 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 URLtoNestedJarFileUrl(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 URLDetermines the parent JAR file's URL for your running application (not including the "!"static StringGenerates 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 StringGenerates 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 oneFileto be compared with the otherFile.aFileB- The otherFileto be compared with the oneFile.- Returns:
- True in case of equality, else false.
- Throws:
FileNotFoundException- thrown in case aFilewas 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:
-
createRandomTempFile
Creates a temporaryFilewith random content as of theRandomTextMode.ASCIIof the given size. The temporaryFileis deleted upon exiting the JVM.- Parameters:
aBytes- The size in bytes of theFileto be created.- Returns:
- The temporary
Fileof the given random content and size. - Throws:
IOException- thrown in case there were I/O related problems creating the file.
-
createRandomTempFile
Creates a temporaryFilewith random content as of theRandomTextModeof the given size.- Parameters:
aBytes- The size in bytes of theFileto be created.isDeleteOnExit- Delete the file upon exit of the JVM.- Returns:
- The temporary
Fileof 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 temporaryFilewith random content as of theRandomTextModeof the given size. The temporaryFileis deleted upon exiting the JVM.- Parameters:
aBytes- The size in bytes of theFileto be created.aRandomTextMode- TheRandomTextModeto use when creating theFile.- Returns:
- The temporary
Fileof 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 temporaryFilewith random content as of theRandomTextModeof the given size.- Parameters:
aBytes- The size in bytes of theFileto be created.aRandomTextMode- TheRandomTextModeto use when creating theFile.isDeleteOnExit- Delete the file upon exit of the JVM.- Returns:
- The temporary
Fileof the given random content and size. - Throws:
IOException- thrown in case there were I/O related problems creating the file.
-
createRandomTempFile
Creates a temporaryFilewith random content as of theRandomTextMode.ASCIIof the given size. The temporaryFileis deleted upon exiting the JVM.- Parameters:
aBaseFileName- The base file name to use.aBytes- The size in bytes of theFileto be created.- Returns:
- The temporary
Fileof 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 temporaryFilewith random content as of theRandomTextModeof the given size.- Parameters:
aBaseFileName- The base file name to use.aBytes- The size in bytes of theFileto be created.isDeleteOnExit- Delete the file upon exit of the JVM.- Returns:
- The temporary
Fileof 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 temporaryFilewith random content as of theRandomTextModeof the given size. The temporaryFileis deleted upon exiting the JVM.- Parameters:
aBaseFileName- The base file name to use.aBytes- The size in bytes of theFileto be created.aRandomTextMode- TheRandomTextModeto use when creating theFile.- Returns:
- The temporary
Fileof 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 temporaryFilewith random content as of theRandomTextModeof the given size.- Parameters:
aBaseFileName- The base file name to use.aBytes- The size in bytes of theFileto be created.aRandomTextMode- TheRandomTextModeto use when creating theFile.isDeleteOnExit- Delete the file upon exit of the JVM.- Returns:
- The temporary
Fileof the given random content and size. - Throws:
IOException- thrown in case there were I/O related problems creating the file.
-
createTempFile
Creates a temporaryFilefilled with content of the specified value with the given size (number of bytes). The temporaryFileis deleted upon exiting the JVM.- Parameters:
aBytes- The size in bytes of theFileto be created.aValue- The value with which to fill the file.- Returns:
- The temporary
Fileof 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 temporaryFilefilled with content of the specified value with the given size (number of bytes). T- Parameters:
aBytes- The size in bytes of theFileto be created.aValue- The value with which to fill the file.isDeleteOnExit- Delete the file upon exit of the JVM.- Returns:
- The temporary
Fileof 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 temporaryFilefilled with content of the specified value with the given size (number of bytes). The temporaryFileis deleted upon exiting the JVM.- Parameters:
aBaseFileName- The base file name to use.aBytes- The size in bytes of theFileto be created.aValue- The value with which to fill the file.- Returns:
- The temporary
Fileof 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 temporaryFilefilled 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 theFileto be created.aValue- The value with which to fill the file.isDeleteOnExit- Delete the file upon exit of the JVM.- Returns:
- The temporary
Fileof 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)Filewhich 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:
-
getResourceAsStream
Provides anInputStreamfor 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
InputStreamfor 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.
-