Package generic.jar

Class ResourceFile

java.lang.Object
generic.jar.ResourceFile
All Implemented Interfaces:
Comparable<ResourceFile>

public class ResourceFile extends Object implements Comparable<ResourceFile>
Class for representing file object regardless of whether they are actual files in the file system or or files stored inside of a jar file. This class provides most all the same capabilities as the File class.
  • Constructor Details

    • ResourceFile

      public ResourceFile(File file)
      Construct a ResourceFile that represents a normal file in the file system.
      Parameters:
      file - the file in the file system.
    • ResourceFile

      public ResourceFile(ResourceFile resourceFile, String path)
      Construct a new ResourceFile from a parent file and a relative child path.
      Parameters:
      resourceFile - the parent file
      path - the child path.
    • ResourceFile

      public ResourceFile(String absolutePath)
      Constructs a Resource file from string path that can be either a file path or a jar url.
      Parameters:
      absolutePath - the path to the file.
    • ResourceFile

      public ResourceFile(String absolutePath, JarEntryFilter filter)
      Constructs a Resource file from string path that can be either a file path or a jar url.
      Parameters:
      absolutePath - the path to the file.
      filter - The filter used to exclude files from being loaded
  • Method Details

    • openJarResourceFile

      public static ResourceFile openJarResourceFile(File jarFile, JarEntryFilter filter) throws IOException
      Creates a new Root ResourceFile for a given jar file.
      Parameters:
      jarFile - the jar file to open.
      filter - JarEntryFilter that will filter out unwanted jar entries.
      Returns:
      A Resource file that represents the root of the jarfile file system.
      Throws:
      IOException - if the jar file can't be read.
    • getAbsolutePath

      public String getAbsolutePath()
      Returns the absolute file path for this file.
      Returns:
      the absolute file path for this file.
    • getCanonicalPath

      public String getCanonicalPath() throws IOException
      Returns the canonical file path for this file.
      Returns:
      the absolute file path for this file.
      Throws:
      IOException - if an exception is thrown getting the canonical path
    • listFiles

      public ResourceFile[] listFiles()
      Returns a array of ResourceFiles if this ResourceFile is a directory. Otherwise return null.
      Returns:
      the child ResourceFiles if this is a directory, null otherwise.
    • listFiles

      public ResourceFile[] listFiles(ResourceFileFilter filter)
      Returns a array of ResourceFiles if this ResourceFile is a directory. Otherwise return null.
      Parameters:
      filter - a filter to restrict the array of files returned.
      Returns:
      the child ResourceFiles if this is a directory, null otherwise.
    • getName

      public String getName()
      Returns the simple name of the file.
      Returns:
      the simple name of the file.
    • isDirectory

      public boolean isDirectory()
      Returns true if this Resource file exists and is a directory.
      Returns:
      true if this Resource file exists and is a directory.
    • getParentFile

      public ResourceFile getParentFile()
      Returns the parent of this ResourceFile or null if it is a root.
      Returns:
      the parent of this ResourceFile or null if it is a root.
    • toURL

      public URL toURL() throws MalformedURLException
      Returns a URL that represents this file object.
      Returns:
      a URL that represents this file object.
      Throws:
      MalformedURLException - if a URL can't be formed for this file.
    • lastModified

      public long lastModified()
      Returns the time that this file was last modified.
      Returns:
      the time that this file was last modified.
    • getInputStream

      public InputStream getInputStream() throws FileNotFoundException, IOException
      If this file exists and is not a directory, it will return an InputStream for the file's contents.
      Returns:
      an InputStream for the file's contents.
      Throws:
      FileNotFoundException - if the file does not exist.
      IOException - if an exception occurs creating the input stream
    • delete

      public boolean delete()
      Attempts to delete the file. Not supported (returns false) for files within a jar file.
      Returns:
      true if the file was deleted, false otherwise.
    • exists

      public boolean exists()
      Returns true if the file exists.
      Returns:
      true if the file exists.
    • getOutputStream

      public OutputStream getOutputStream() throws FileNotFoundException
      Returns an OutputStream if the file can be opened for writing.
      Returns:
      an OutputStream if the file can be opened for writing.
      Throws:
      FileNotFoundException - if the file can't be created or opened for writing.
    • getFile

      public File getFile(boolean copyIfNeeded)
      Returns a File object. If this ResourceFile represents a standard filesystem, then no copy is necessary to return a file. If this ResourceFile represents a compressed filesystem, then a copy from that filesystem to the real filesystem is needed to create a File object. copyIfNeeded allows you to dictate whether a copy should take place, if needed.

      If you just want the contents of a file, then call getInputStream().

      Parameters:
      copyIfNeeded - true to copy the file when embedded in a compressed filesystem; false to return null in that case.
      Returns:
      a File object or null if not a file and copyIfNeeded was false
    • length

      public long length()
      Returns the size of this file.
      Returns:
      the size of the file.
    • isFile

      public boolean isFile()
      Returns true if this file exists and is not a directory.
      Returns:
      true if this file exists and is not a directory.
    • getCanonicalFile

      public ResourceFile getCanonicalFile()
      Returns the canonicalFile for this file.
      Returns:
      the canonicalFile for this file.
    • canWrite

      public boolean canWrite()
      Returns true if this file can be written to.
      Returns:
      true if this file can be written to.
    • mkdir

      public boolean mkdir()
      Creates a directory for the path represented by this file.
      Returns:
      true if a new directory was created.
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • compareTo

      public int compareTo(ResourceFile o)
      Specified by:
      compareTo in interface Comparable<ResourceFile>
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • getFileSystemRoot

      public File getFileSystemRoot()
      Returns the root file for this file.
      Returns:
      the root file for this file.
    • toURI

      public URI toURI()
      Returns a URI for this file object.
      Returns:
      a URI for this file object.
    • containsPath

      public boolean containsPath(ResourceFile otherFile)
      Returns true if this file's path contains the entire path of the given file.
      Parameters:
      otherFile - the other file to check
      Returns:
      true if this file's path contains the entire path of the given file.