Package generic.jar
Class ResourceFile
- java.lang.Object
-
- generic.jar.ResourceFile
-
- All Implemented Interfaces:
java.lang.Comparable<ResourceFile>
public class ResourceFile extends java.lang.Object implements java.lang.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
Constructors Constructor Description ResourceFile(ResourceFile resourceFile, java.lang.String path)
Construct a new ResourceFile from a parent file and a relative child path.ResourceFile(java.io.File file)
Construct a ResourceFile that represents a normal file in the file system.ResourceFile(java.lang.String absolutePath)
Constructs a Resource file from string path that can be either a file path or a jar url.ResourceFile(java.lang.String absolutePath, JarEntryFilter filter)
Constructs a Resource file from string path that can be either a file path or a jar url.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
canWrite()
Returns true if this file can be written to.int
compareTo(ResourceFile o)
boolean
delete()
Attempts to delete the file.boolean
equals(java.lang.Object obj)
boolean
exists()
Returns true if the file exists.java.lang.String
getAbsolutePath()
Returns the absolute file path for this file.ResourceFile
getCanonicalFile()
Returns the canonicalFile for this file.java.lang.String
getCanonicalPath()
Returns the canonical file path for this file.java.io.File
getFile(boolean copyIfNeeded)
Returns a File object.java.io.File
getFileSystemRoot()
Returns the root file for this file.java.io.InputStream
getInputStream()
If this file exists and is not a directory, it will return an InputStream for the file's contents.java.lang.String
getName()
Returns the simple name of the file.java.io.OutputStream
getOutputStream()
Returns an OutputStream if the file can be opened for writing.ResourceFile
getParentFile()
Returns the parent of this ResourceFile or null if it is a root.int
hashCode()
boolean
isDirectory()
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
lastModified()
Returns the time that this file was last modified.long
length()
Returns the size of this file.ResourceFile[]
listFiles()
Returns a array of ResourceFiles if this ResourceFile is a directory.ResourceFile[]
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(java.io.File jarFile, JarEntryFilter filter)
Creates a new Root ResourceFile for a given jar file.java.lang.String
toString()
java.net.URI
toURI()
Returns a URI for this file object.java.net.URL
toURL()
Returns a URL that represents this file object.
-
-
-
Constructor Detail
-
ResourceFile
public ResourceFile(java.io.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, java.lang.String path)
Construct a new ResourceFile from a parent file and a relative child path.- Parameters:
resourceFile
- the parent filepath
- the child path.
-
ResourceFile
public ResourceFile(java.lang.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(java.lang.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 Detail
-
openJarResourceFile
public static ResourceFile openJarResourceFile(java.io.File jarFile, JarEntryFilter filter) throws java.io.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:
java.io.IOException
- if the jar file can't be read.
-
getAbsolutePath
public java.lang.String getAbsolutePath()
Returns the absolute file path for this file.- Returns:
- the absolute file path for this file.
-
getCanonicalPath
public java.lang.String getCanonicalPath() throws java.io.IOException
Returns the canonical file path for this file.- Returns:
- the absolute file path for this file.
- Throws:
java.io.IOException
-
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 java.lang.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 java.net.URL toURL() throws java.net.MalformedURLException
Returns a URL that represents this file object.- Returns:
- a URL that represents this file object.
- Throws:
java.net.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 java.io.InputStream getInputStream() throws java.io.FileNotFoundException, java.io.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:
java.io.FileNotFoundException
- if the file does not exist.java.io.IOException
-
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 java.io.OutputStream getOutputStream() throws java.io.FileNotFoundException
Returns an OutputStream if the file can be opened for writing.- Returns:
- an OutputStream if the file can be opened for writing.
- Throws:
java.io.FileNotFoundException
- if the file can't be created or opened for writing.
-
getFile
public java.io.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(java.lang.Object obj)
- Overrides:
equals
in classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
compareTo
public int compareTo(ResourceFile o)
- Specified by:
compareTo
in interfacejava.lang.Comparable<ResourceFile>
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
getFileSystemRoot
public java.io.File getFileSystemRoot()
Returns the root file for this file.- Returns:
- the root file for this file.
-
toURI
public java.net.URI toURI()
Returns a URI for this file object.- Returns:
- a URI for this file object.
-
-