Package generic.jar
Class ResourceFile
java.lang.Object
generic.jar.ResourceFile
- All Implemented Interfaces:
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 Summary
ConstructorDescriptionResourceFile
(ResourceFile resourceFile, String path) Construct a new ResourceFile from a parent file and a relative child path.ResourceFile
(File file) Construct a ResourceFile that represents a normal file in the file system.ResourceFile
(String absolutePath) Constructs a Resource file from string path that can be either a file path or a jar url.ResourceFile
(String absolutePath, JarEntryFilter filter) Constructs a Resource file from string path that can be either a file path or a jar url. -
Method Summary
Modifier and TypeMethodDescriptionboolean
canWrite()
Returns true if this file can be written to.int
boolean
containsPath
(ResourceFile otherFile) Returns true if this file's path contains the entire path of the given file.boolean
delete()
Attempts to delete the file.boolean
boolean
exists()
Returns true if the file exists.Returns the absolute file path for this file.Returns the canonicalFile for this file.Returns the canonical file path for this file.getFile
(boolean copyIfNeeded) Returns a File object.Returns the root file for this file.If this file exists and is not a directory, it will return an InputStream for the file's contents.getName()
Returns the simple name of the file.Returns an OutputStream if the file can be opened for writing.Returns the parent of this ResourceFile or null if it is a root.int
hashCode()
boolean
Returns true if this Resource file exists and is a directory.boolean
isFile()
Returns true if this file exists and is not a directory.long
Returns the time that this file was last modified.long
length()
Returns the size of this file.Returns a array of ResourceFiles if this ResourceFile is a directory.listFiles
(ResourceFileFilter filter) Returns a array of ResourceFiles if this ResourceFile is a directory.boolean
mkdir()
Creates a directory for the path represented by this file.static ResourceFile
openJarResourceFile
(File jarFile, JarEntryFilter filter) Creates a new Root ResourceFile for a given jar file.toString()
toURI()
Returns a URI for this file object.toURL()
Returns a URL that represents this file object.
-
Constructor Details
-
ResourceFile
Construct a ResourceFile that represents a normal file in the file system.- Parameters:
file
- the file in the file system.
-
ResourceFile
Construct a new ResourceFile from a parent file and a relative child path.- Parameters:
resourceFile
- the parent filepath
- the child path.
-
ResourceFile
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
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
Returns the absolute file path for this file.- Returns:
- the absolute file path for this file.
-
getCanonicalPath
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
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
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
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
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
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
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
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
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
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
-
hashCode
public int hashCode() -
compareTo
- Specified by:
compareTo
in interfaceComparable<ResourceFile>
-
toString
-
getFileSystemRoot
Returns the root file for this file.- Returns:
- the root file for this file.
-
toURI
Returns a URI for this file object.- Returns:
- a URI for this file object.
-
containsPath
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.
-