Class FileUtility

java.lang.Object
org.refcodes.io.FileUtility

public final class FileUtility extends Object
The FileUtility provides Various file related utility functionality.
  • Method Summary

    Modifier and Type
    Method
    Description
    static 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 temporary File with random content as of the RandomTextMode.ASCII of the given size.
    static File
    createRandomTempFile(long aBytes, boolean isDeleteOnExit)
    Creates a temporary File with random content as of the RandomTextMode of the given size.
    static File
    createRandomTempFile(long aBytes, org.refcodes.textual.RandomTextMode aRandomTextMode)
    Creates a temporary File with random content as of the RandomTextMode of the given size.
    static File
    createRandomTempFile(long aBytes, org.refcodes.textual.RandomTextMode aRandomTextMode, boolean isDeleteOnExit)
    Creates a temporary File with random content as of the RandomTextMode of the given size.
    static File
    createRandomTempFile(String aBaseFileName, long aBytes)
    Creates a temporary File with random content as of the RandomTextMode.ASCII of the given size.
    static File
    createRandomTempFile(String aBaseFileName, long aBytes, boolean isDeleteOnExit)
    Creates a temporary File with random content as of the RandomTextMode of the given size.
    static File
    createRandomTempFile(String aBaseFileName, long aBytes, org.refcodes.textual.RandomTextMode aRandomTextMode)
    Creates a temporary File with random content as of the RandomTextMode of the given size.
    static File
    createRandomTempFile(String aBaseFileName, long aBytes, org.refcodes.textual.RandomTextMode aRandomTextMode, boolean isDeleteOnExit)
    Creates a temporary File with random content as of the RandomTextMode of the given size.
    static File
    Creates an empty temporary File.
    static File
    createTempFile(boolean isDeleteOnExit)
    Creates an empty temporary File.
    static File
    createTempFile(long aBytes, byte aValue)
    Creates a temporary File 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 temporary File filled with content of the specified value with the given size (number of bytes).
    static File
    createTempFile(String aBaseFileName)
    Creates an empty temporary File.
    static File
    createTempFile(String aBaseFileName, boolean isDeleteOnExit)
    Creates an empty temporary File.
    static File
    createTempFile(String aBaseFileName, long aBytes, byte aValue)
    Creates a temporary File 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 temporary File 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.
    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 boolean
    isEqual(File aFileA, File aFileB)
    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 via createTempFile(String), createTempFile(String, boolean), createTempFile(String, long, byte) or createTempFile(String, long, byte, boolean) (make sure to use the same base filename when opening as has been used when creating).
    static String[]
    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".

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • isEqual

      public static boolean isEqual(File aFileA, File aFileB) throws FileNotFoundException, IOException
      Determines equality of the provided files by comparing their content (payload).
      Parameters:
      aFileA - The one File to be compared with the other File.
      aFileB - The other File to be compared with the one File.
      Returns:
      True in case of equality, else false.
      Throws:
      FileNotFoundException - thrown in case a File was not found.
      IOException - thrown in case there were I/O related problems.
    • createNestedJarFileUrl

      public static URL createNestedJarFileUrl(URL aJarUrl, File aToDir) throws IOException
      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

      public static File createRandomTempFile(long aBytes) throws IOException
      Creates a temporary File with random content as of the RandomTextMode.ASCII of the given size. The temporary File is deleted upon exiting the JVM.
      Parameters:
      aBytes - The size in bytes of the File 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(long aBytes, boolean isDeleteOnExit) throws IOException
      Creates a temporary File with random content as of the RandomTextMode of the given size.
      Parameters:
      aBytes - The size in bytes of the File 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 temporary File with random content as of the RandomTextMode of the given size. The temporary File is deleted upon exiting the JVM.
      Parameters:
      aBytes - The size in bytes of the File to be created.
      aRandomTextMode - The RandomTextMode to use when creating the File.
      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 temporary File with random content as of the RandomTextMode of the given size.
      Parameters:
      aBytes - The size in bytes of the File to be created.
      aRandomTextMode - The RandomTextMode to use when creating the File.
      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) throws IOException
      Creates a temporary File with random content as of the RandomTextMode.ASCII of the given size. The temporary File is deleted upon exiting the JVM.
      Parameters:
      aBaseFileName - The base file name to use.
      aBytes - The size in bytes of the File 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 temporary File with random content as of the RandomTextMode of the given size.
      Parameters:
      aBaseFileName - The base file name to use.
      aBytes - The size in bytes of the File 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 temporary File with random content as of the RandomTextMode of the given size. The temporary File is deleted upon exiting the JVM.
      Parameters:
      aBaseFileName - The base file name to use.
      aBytes - The size in bytes of the File to be created.
      aRandomTextMode - The RandomTextMode to use when creating the File.
      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 temporary File with random content as of the RandomTextMode of the given size.
      Parameters:
      aBaseFileName - The base file name to use.
      aBytes - The size in bytes of the File to be created.
      aRandomTextMode - The RandomTextMode to use when creating the File.
      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

      public static File createTempFile(long aBytes, byte aValue) throws IOException
      Creates a temporary File filled with content of the specified value with the given size (number of bytes). The temporary File is deleted upon exiting the JVM.
      Parameters:
      aBytes - The size in bytes of the File 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 temporary File filled with content of the specified value with the given size (number of bytes). T
      Parameters:
      aBytes - The size in bytes of the File 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 temporary File filled with content of the specified value with the given size (number of bytes). The temporary File is deleted upon exiting the JVM.
      Parameters:
      aBaseFileName - The base file name to use.
      aBytes - The size in bytes of the File 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 temporary File 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 the File 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() throws IOException
      Creates an empty temporary File. The temporary File is deleted upon exiting the JVM.
      Returns:
      The temporary File.
      Throws:
      IOException - thrown in case there were I/O related problems creating the file.
    • createTempFile

      public static File createTempFile(boolean isDeleteOnExit) throws IOException
      Creates an empty temporary File.
      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

      public static File createTempFile(String aBaseFileName) throws IOException
      Creates an empty temporary File. The temporary File is deleted upon exiting the JVM.
      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

      public static File createTempFile(String aBaseFileName, boolean isDeleteOnExit) throws IOException
      Creates an empty temporary File.
      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

      public static File openTempFile(String aBaseFileName) throws IOException
      Opens a temporary (existing) File which has been created via createTempFile(String), createTempFile(String, boolean), createTempFile(String, long, byte) or createTempFile(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

      public static URL getNestedJarFileUrl(URL aJarUrl, File aToDir) throws IOException
      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 of 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.
      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

      public 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).
      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

      public 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. 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

      public static URL toNestedJarFileUrl(URL aJarUrl, File aToDir) throws IOException
      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

      public static URL 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

      public static String 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

      public static String 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.